0

我目前正在尝试使用 PowerMock 1.4.10 对一些遗留代码进行一些单元测试,并且我正在尝试测试一种同时具有静态 void 调用和静态非 void 调用的方法。有没有办法在抑制静态 void 调用的同时模拟静态非 void 调用?

@RunWith(PowerMockRunner.class)
@PrepareForTest(ClassUnderTest.class)
public class TestClass {

    @Test
    public void test() {
        MemberModifier.suppress(StaticClass.class.getDeclaredMethod("logRandom"));

        PowerMock.mockStatic(StaticClass.class);
        expect(StaticClass.isLogOn()).andReturn(true);
        PowerMock.replay(StaticClass.class);

        ClassUnderTest test = new ClassUnderTest();
        test.methodToTest();

        assertTrue(test.tested);

        PowerMock.verify(StaticClass.class);
    }
}


public class ClassUnderTest {
    public boolean tested = false;

    public void methodToTest() {
        if (StaticClass.isLogOn()) {
            StaticClass.logRandom();
        }
    }

}

public class StaticClass {

    public static void logRandom() {
        System.out.println("Do something");
    }

    public static boolean isLogOn() {
        return (Math.random() > .5);
    }
}

但是当我尝试这样做时,我得到以下 stackTrace:

java.lang.IllegalStateException: no last call on a mock available
    at org.easymock.EasyMock.getControlForLastCall(EasyMock.java:521)
    at org.easymock.EasyMock.expect(EasyMock.java:499)
    at TestClass.test(TestClass.java:20)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.junit.internal.runners.TestMethod.invoke(TestMethod.java:68)
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.runTestMethod(PowerMockJUnit44RunnerDelegateImpl.java:307)
    at org.junit.internal.runners.MethodRoadie$2.run(MethodRoadie.java:88)
    at org.junit.internal.runners.MethodRoadie.runBeforesThenTestThenAfters(MethodRoadie.java:96)
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.executeTest(PowerMockJUnit44RunnerDelegateImpl.java:294)
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.runBeforesThenTestThenAfters(PowerMockJUnit44RunnerDelegateImpl.java:282)
    at org.junit.internal.runners.MethodRoadie.runTest(MethodRoadie.java:86)
    at org.junit.internal.runners.MethodRoadie.run(MethodRoadie.java:49)
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.invokeTestMethod(PowerMockJUnit44RunnerDelegateImpl.java:207)
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.runMethods(PowerMockJUnit44RunnerDelegateImpl.java:146)
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$1.run(PowerMockJUnit44RunnerDelegateImpl.java:120)
    at org.junit.internal.runners.ClassRoadie.runUnprotected(ClassRoadie.java:33)
    at org.junit.internal.runners.ClassRoadie.runProtected(ClassRoadie.java:45)
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.run(PowerMockJUnit44RunnerDelegateImpl.java:118)
    at org.powermock.modules.junit4.common.internal.impl.JUnit4TestSuiteChunkerImpl.run(JUnit4TestSuiteChunkerImpl.java:102)
    at org.powermock.modules.junit4.common.internal.impl.AbstractCommonPowerMockRunner.run(AbstractCommonPowerMockRunner.java:53)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:49)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)

有没有人对如何处理这个问题有任何想法?

4

1 回答 1

1

我使用 PowerMock 和 Mockito 的组合(称为 PowerMockito)成功地测试了静力学。PowerMockito.mockStatic您将使用而不是版本来模拟您的静态调用PowerMock。完成后,您可以打电话Mockito.when来处理电话。

PowerMockito.mockStatic(StaticClass.class);
Mockito.when(StaticClass.isLogOn()).thenReturn(true);

当测试运行时,当它到达测试执行时if (StaticClass.isLogOn()) {,模拟应该介入并返回 true,这将进入调用logRandom. 从这里,您可以尝试使用spy让它调用实际代码,或者您可以logRandom根据需要模拟该调用。您可以spyPowerMocktio 页面下找到有关使用模拟的更多信息Partial Mocking

于 2013-04-08T15:53:07.220 回答