8

I tried out the mock-maker-inline "Incubation" feature of Mockito to be able to mock a final class (problem described and discussed here). Since then other tests fail with:

org.mockito.exceptions.misusing.NotAMockException: 
Argument passed to when() is not a mock!
Example of correct stubbing:
    doThrow(new RuntimeException()).when(mock).someMethod();

when I define an exception to be thrown by a spy. Relevant code from one of the tests:

In @Configuration class:

@Bean
public MessagePersister messagePersister() {
    return Mockito.spy(new MessagePersister(...));
}

Note: MessagePersister is proxied by CGLIB.

In Test class:

@Inject
private MessagePersister messagePersisterSpy;

@Test
public void exceptionInPersisterTest() {
    doThrow(new SomeException("exceptionFromTest")).doCallRealMethod()
            .when(messagePersisterSpy).persistMessages(any());
...
}

The exception is understandable. The class of messagePersisterSpy is MessagePersister$$EnhancerBySpringCGLIB$$6c49f1e2, but if I remove the mock-maker-inline feature, my spy is of class MessagePersister$MockitoMock$515952708$$EnhancerBySpringCGLIB$$9523b504 and the tests are green.

Any ideas where this interference comes from and if I can do something about it ?

Thank you !

Tchypp
  • 1,075
  • 2
  • 13
  • 20
  • 1
    I have exactly the same problem... Could you finally find a workaround/fix? Any feedback would be appreciated. – teemoo Jul 15 '19 at 07:55
  • 1
    @teemoo Sorry, but I don't remember exactly. As I don't find any use of `mock-maker-inline` in the project, I guess I dropped it all together and redesigned the test / code. – Tchypp Jul 24 '19 at 09:36
  • @teemoo figured out any solution? – M-sAnNan Dec 02 '22 at 13:28
  • @M-sAnNan nope, sorry, I think I also decided to rewrite the code to test it in a more regular way with standard Mockito. – teemoo Dec 05 '22 at 07:49

0 Answers0