使用PowerMock在同一类中抑制void静态方法和静态方法

时间:2013-04-04 17:58:36

标签: java junit mocking powermock

我目前正在尝试使用PowerMock 1.4.10对一些遗留代码进行一些单元测试,并且我正在尝试测试一种具有静态无效调用和静态非空调用的方法。有没有办法在抑制静态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)

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

1 个答案:

答案 0 :(得分: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调用。您可以在spy下的PowerMocktio Page上找到有关使用Partial Mocking进行模拟的更多信息。