junit Powermock:静态接口方法给出未完成的调试异常

5jdjgkvh  于 11个月前  发布在  其他
关注(0)|答案(2)|浏览(199)

我想用Powermock模拟一个静态接口方法。下面是接口:

public interface IConcurrentUtil {
    static void threadSleep(final long millis) {
        try {
            Thread.sleep(millis);
        } catch (final InterruptedException e) {
            ;
        }
    }
}

字符串
下面是使用threadSleep的类:

public class ConcurrentUser {

    public void callInterfaceMethod() {
        IConcurrentUtil.threadSleep(3000L);
    }
}


最后是测试类:

@RunWith(PowerMockRunner.class)
@PrepareForTest(ConcurrentUser.class)
@PowerMockIgnore("javax.management.*")
public class ConcurrentUtilsTest {

    private ConcurrentUser concurrentUser;

    @Before
    public void setUp() {
        concurrentUser = new ConcurrentUser();
    }

    @Test
    public void testThreadSleepCallsSleepCorrect() throws Exception {
        mockStatic(IConcurrentUtil.class);
        doNothing().when(IConcurrentUtil.class);
        IConcurrentUtil.threadSleep(3000L);

        concurrentUser.callInterfaceMethod();

        verifyStatic(times(1));
        IConcurrentUtil.threadSleep(3000L);
    }
}


Powermock给出了这个错误:

org.mockito.exceptions.misusing.UnfinishedStubbingException: 
Unfinished stubbing detected here:
-> at org.powermock.api.mockito.internal.PowerMockitoCore.doAnswer(PowerMockitoCore.java:36)

E.g. thenReturn() may be missing.
Examples of correct stubbing:
    when(mock.isOk()).thenReturn(true);
    when(mock.isOk()).thenThrow(exception);
    doThrow(exception).when(mock).someVoidMethod();
Hints:
 1. missing thenReturn()
 2. you are trying to stub a final method, you naughty developer!
 3: you are stubbing the behaviour of another mock inside before 'thenReturn' instruction if completed

    at org.powermock.api.mockito.PowerMockito.verifyStatic(PowerMockito.java:288)
    at com.test.concurrent.ConcurrentUtilsTest.testThreadSleepCallsSleepCorrect(ConcurrentUtilsTest.java:35)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:483)
    at org.junit.internal.runners.TestMethod.invoke(TestMethod.java:68)
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.runTestMethod(PowerMockJUnit44RunnerDelegateImpl.java:310)
    at org.junit.internal.runners.MethodRoadie$2.run(MethodRoadie.java:89)
    at org.junit.internal.runners.MethodRoadie.runBeforesThenTestThenAfters(MethodRoadie.java:97)
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.executeTest(PowerMockJUnit44RunnerDelegateImpl.java:294)
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit47RunnerDelegateImpl$PowerMockJUnit47MethodRunner.executeTestInSuper(PowerMockJUnit47RunnerDelegateImpl.java:127)
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit47RunnerDelegateImpl$PowerMockJUnit47MethodRunner.executeTest(PowerMockJUnit47RunnerDelegateImpl.java:82)
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.runBeforesThenTestThenAfters(PowerMockJUnit44RunnerDelegateImpl.java:282)
    at org.junit.internal.runners.MethodRoadie.runTest(MethodRoadie.java:87)
    at org.junit.internal.runners.MethodRoadie.run(MethodRoadie.java:50)
    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:34)
    at org.junit.internal.runners.ClassRoadie.runProtected(ClassRoadie.java:44)
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.run(PowerMockJUnit44RunnerDelegateImpl.java:122)
    at org.powermock.modules.junit4.common.internal.impl.JUnit4TestSuiteChunkerImpl.run(JUnit4TestSuiteChunkerImpl.java:106)
    at org.powermock.modules.junit4.common.internal.impl.AbstractCommonPowerMockRunner.run(AbstractCommonPowerMockRunner.java:53)
    at org.powermock.modules.junit4.PowerMockRunner.run(PowerMockRunner.java:59)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)


PowerMock 1.6.2和JUnit 4.12
在处理静态(或默认)接口方法时,是否需要应用任何特殊的规则或命令?

8fsztsew

8fsztsew1#

实际上,在您的情况下,您不需要做编译,因为mockStatic已经为您做了这件事,它只是多余的。
所以,下面的工作:

@RunWith(PowerMockRunner.class)
@PrepareForTest(IConcurrentUtil.class)
@PowerMockIgnore("javax.management.*")
public class ConcurrentUtilTest {

    private ConcurrentUser concurrentUser;

    @Before
    public void setUp() {
        concurrentUser = new ConcurrentUser();
    }

    @Test
    public void testThreadSleepCallsSleepCorrect() throws Exception {
        PowerMockito.mockStatic(IConcurrentUtil.class);

        concurrentUser.callInterfaceMethod();

        PowerMockito.verifyStatic(Mockito.times(1));
        IConcurrentUtil.threadSleep(3000L);
    }
}

字符串
但如果你真的想做一些修改,这里有一个例子,适用于你正在使用的版本:

PowerMockito.mockStatic(FileUtils.class, Answers.CALLS_REAL_METHODS.get());
PowerMockito.doThrow(new IOException()).when(FileUtils.class, "copyFile", any(File.class), any(File.class));


在这里,我使用2-args mockStatic签名,因为我想做部分删除:真实的方法在FileUtils类上被调用,除非我像我在第二行中所做的那样对它进行存根。如果你不介意所有的静态方法都被默认的答案存根,你也可以使用1-arg版本。
你也可以看看this answerthis one

pdsfdshx

pdsfdshx2#

由于我忘记在我的测试类中添加@PrepareForTest({MyClass.class}) annotation,所以我在PowerMockito's mockStatic(MyClass.class)中得到了这个异常。
添加后,它开始工作

相关问题