Java Powermock:静态接口方法给出未完成的存根异常
我想用Powermock模拟一个静态接口方法。 以下是界面:Java Powermock:静态接口方法给出未完成的存根异常,java,junit,interface,static,powermock,Java,Junit,Interface,Static,Powermock,我想用Powermock模拟一个静态接口方法。 以下是界面: public interface IConcurrentUtil { static void threadSleep(final long millis) { try { Thread.sleep(millis); } catch (final InterruptedException e) { ; } } } 下面是使用
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
在处理静态(或默认)接口方法时,是否需要应用任何特殊规则或命令?实际上,在您的情况下,您不需要执行存根,因为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签名是因为我想进行部分stubing:除非我像在第二行中那样对FileUtils类进行stub,否则实方法将被调用。如果您不介意所有静态方法都使用默认答案,那么也可以使用1-arg版本
您还可以查看和。谢谢您的回答,但是我需要如何更改testThreadSleepCallsSleepCorrect以消除错误消息?@johnmartel解决方法很好。但是如果我真的想存根一个接口方法呢?当我尝试这样做时,我得到了同样的例外。在我将接口更改为类之后,stubing处理得很好。这是一个已知的限制吗?它似乎确实是一个限制。我们的朋友@juxeii首先提出了这个问题,他在PowerMock中提出了一个问题:。关于Java8中添加的静态接口方法的有趣讨论,请参见此处:似乎
会回答.CALLS\u REAL\u methods.get()
完成这项工作。没有它,即使使用正确的doThrow()
,我总是会得到未完成的存根异常。