调用真实的方法,但在静态方法执行时给出不同的返回答案



我正在使用power mock,并且我正在模拟具有所有静态方法的类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 原始类型。我遗漏了什么吗?请指导

我认为您在测试类(@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));
    }
}

相关内容

  • 没有找到相关文章

最新更新