虽然给出了不同的答案来调用静态方法执行,但调用实际方法

时间:2014-07-10 11:29:57

标签: junit mockito powermock

我正在使用power mockito,我正在嘲笑一个具有所有静态方法的类SomeUtil

import java.util.List;

class SomeUtil {

    // other static methods

    public static X createX(String name, List<String> addresses, boolean isEnabled) {
        // implementation
        return null;
    }

    // other static methods

}

我嘲笑如下。

PowerMockito.mockStatic(SomeUtil.class, Answers.CALLS_REAL_METHODS.get());
Answer<Row> createXAnswer = new Answer<Row>() {
            @Override
            public Row answer(InvocationOnMock invocation) throws Throwable {
                return new X();
            }
        };
PowerMockito.when(SomeUtil.createX(Mockito.any(String.class), Mockito.any(List.class), Mockito.any(Boolean.class)).thenAnswer(createXAnswer);

但是使用createX()方法总是调用原始方法,我试图避免这种方法。我怀疑是因为boolean 原始类型。我在这里错过了什么吗?请指导

1 个答案:

答案 0 :(得分:2)

我认为您在测试类(@RunWith@PrepareForTest)上省略了一些重要的注释。这是一个完整的例子:

import static org.junit.Assert.assertSame;
import static org.mockito.Matchers.anyBoolean;
import static org.mockito.Matchers.anyList;
import static org.mockito.Matchers.anyString;
import static org.powermock.api.mockito.PowerMockito.mockStatic;
import static org.powermock.api.mockito.PowerMockito.when;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;

@RunWith(PowerMockRunner.class)
@PrepareForTest(SomeUtil.class)
public class SomeUtilTest {
    @Test
    public void should_do_this() {
        mockStatic(SomeUtil.class, Mockito.CALLS_REAL_METHODS);
        final X x = new X();

        when(SomeUtil.createX(anyString(), anyList(), anyBoolean())).thenReturn(x);

        assertSame(x, SomeUtil.createX(null, null, true));
    }
}

此示例有效但在某些情况下可能。例如,如果createX引发异常。有时会失败,因为当我们存根时会调用**实际方法createX **:

when(SomeUtil.createX(anyString(), anyList(), anyBoolean())).thenAnswer(createXAnswer);

这是由于默认答案CALLS_REAL_METHODS

mockStatic(SomeUtil.class, Mockito.CALLS_REAL_METHODS);

因为OP无法删除此默认答案(请参阅注释):

  

只有少数几种方法我想回复自己的答案。对于其他方法,我想称之为真正的方法。

我认为你必须使用powermock api:

import static org.junit.Assert.assertSame;
import static org.powermock.api.mockito.PowerMockito.mockStatic;
import static org.powermock.api.support.membermodification.MemberMatcher.method;
import static org.powermock.api.support.membermodification.MemberModifier.stub;

import java.util.List;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;

@RunWith(PowerMockRunner.class)
@PrepareForTest(SomeUtil.class)
public class SomeUtilTest {
    @Test
    public void should_do_this() {
        mockStatic(SomeUtil.class, Mockito.CALLS_REAL_METHODS);
        final X x = new X();

        stub(method(SomeUtil.class,
                "createX",
                String.class,
                List.class,
                boolean.class)).toReturn(x);


        assertSame(x, SomeUtil.createX(null, null, true));
    }
}