-1

I have the following test file using Mockito framework:

    @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() line returns:

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

I tried different approaches to testing this error throwing behaviour, but I just cannot get rid of this error message which makes my test fail. Any help will be substantially appreciated!

Vlad Balanescu
  • 664
  • 5
  • 27
  • Maybe I am mistaken here - why are you **invoking** your "method under test" ( ` jExt.extract(path); `) **before** you specify the mock behavior?! – GhostCat Feb 21 '18 at 12:30

1 Answers1

1

With your code as written, I added in the following stub for JarExtractor, and the code ran fine, giving the IOException you'd expect:

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

    }
}

Replacing this with the following, I get the same UnfinishedStubbingException as you:

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

    }
}

---edit--- Likewise if the method is static:

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

    }
}

As you say in your comment, your method is static, so you won't be able to mock it with Mockito. Here's some good advice on ways forward for dealing with final methods, and here's some on mocking static methods.

hugh
  • 2,237
  • 1
  • 12
  • 25