Powermock:静态接口方法给出未完成的存根异常
Powermock: Static interface method gives unfinished stubbing exception
我想用 Powermock 模拟一个静态接口方法。
这是界面:
public interface IConcurrentUtil {
static void threadSleep(final long millis) {
try {
Thread.sleep(millis);
} catch (final InterruptedException e) {
;
}
}
}
这是使用 threadSleep 的 class:
public class ConcurrentUser {
public void callInterfaceMethod() {
IConcurrentUtil.threadSleep(3000L);
}
}
最后是测试 class:
@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.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.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。
在处理静态(或默认)接口方法时是否需要应用任何特殊规则或命令?
实际上在你的情况下,你不需要做存根,因为 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 class 上调用,除非我存根,就像我在第二行中所做的那样。如果您不介意所有静态方法都被默认答案存根,您也可以使用 1-arg 版本。
你也可以看看this answer and this one。
我想用 Powermock 模拟一个静态接口方法。 这是界面:
public interface IConcurrentUtil {
static void threadSleep(final long millis) {
try {
Thread.sleep(millis);
} catch (final InterruptedException e) {
;
}
}
}
这是使用 threadSleep 的 class:
public class ConcurrentUser {
public void callInterfaceMethod() {
IConcurrentUtil.threadSleep(3000L);
}
}
最后是测试 class:
@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.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.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。
在处理静态(或默认)接口方法时是否需要应用任何特殊规则或命令?
实际上在你的情况下,你不需要做存根,因为 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 class 上调用,除非我存根,就像我在第二行中所做的那样。如果您不介意所有静态方法都被默认答案存根,您也可以使用 1-arg 版本。
你也可以看看this answer and this one。