我使用Mockito框架有以下测试文件:
@Rule
public ExpectedException expectedException = ExpectedException.none();
@Spy
private JarExtracter jExt = Mockito.spy(JarExtracter.class);
@Test
public void inputStreamTest() throws IOException {
String path = "/edk.dll";
// Call the method and do the checks
jExt.extract(path);
// Check for error with a null stream
path = "/../edk.dll";
doThrow(IOException.class).when(jExt).extract(path);
jExt.extract(path);
verifyNoMoreInteractions(jExt);
expectedException.expect(IOException.class);
expectedException.expectMessage("Cannot get");
doThrow()
线返回:
org.mockito.exceptions.misusing.UnfinishedStubbingException:
Unfinished stubbing detected here:
-> at confusionIndicator.tests.JarExtracterTests.inputStreamTest(JarExtracterTests.java:30)
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
我尝试了测试这种错误行为的不同方法,但是我无法摆脱此错误消息,这使我的测试失败。任何帮助将不胜感激!
使用您的代码,我在JarExtractor
的以下存根中添加了代码,并且代码正常,给出了您期望的IOException:
class JarExtracter {
public void extract(String path) throws IOException{
}
}
用以下内容替换它,我得到了与您相同的未完成的示威:
class JarExtracter {
final public void extract(String path) throws IOException{
}
}
---编辑---同样,如果该方法是静态的:
class JarExtracter {
public static void extract(String path) throws IOException{
}
}
正如您在评论中所说的那样,您的方法是静态的,因此您将无法使用Mockito嘲笑它。这是一些关于处理最终方法的前进方法的好建议,这里有一些关于嘲笑静态方法的建议。