其他分享
首页 > 其他分享> > PowerMock:静态接口方法给出未完成的存根异常

PowerMock:静态接口方法给出未完成的存根异常

作者:互联网

我想用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.

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

解决方法:

实际上,在您的情况下,您不需要进行存根,因为嘲笑Static已经为您完成了,只是多余的.

因此,以下方法将起作用:

@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.

标签:interface,junit,static,powermock,java
来源: https://codeday.me/bug/20191120/2041319.html