Java 为什么我在Mockito中得到一个UnifinishedStubException,而存根看起来与文档中的一样?

Java 为什么我在Mockito中得到一个UnifinishedStubException,而存根看起来与文档中的一样?,java,testing,mockito,tdd,stub,Java,Testing,Mockito,Tdd,Stub,我有以下使用Mockito框架的测试文件: @Rule public ExpectedException expectedException = ExpectedException.none(); @Spy private JarExtracter jExt = Mockito.spy(JarExtracter.class); @Test public void inputStreamTest() throws IOException

我有以下使用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{

    }
}
将此替换为以下内容,我将得到与您相同的未完成StubbingException:

class JarExtracter {
    final public void extract(String path) throws IOException{

    }
}
---编辑---如果方法是静态的,同样:

class JarExtracter {
    public static void extract(String path) throws IOException{

    }
}
正如你在评论中所说,你的方法是静态的,所以你不能用Mockito来模拟它。
这里有一些关于前进方向的好建议,这里也有一些相关的建议。

不,它是:
publicstaticvoidextract(stringresourcename)抛出IOException{
Ah,你也不能用Mockito模拟静态方法。我将把它添加到我的答案中。也许我弄错了-你为什么要调用你的“测试中的方法”(`jExt.extract(path);`)在你指定模拟行为之前?!