junit - Mockito: how to verify method was called inside another method, which always throws Exception? -


i testing method expected exception. need verify code called (on mocked object) after exception thrown, verification being ignored. here code:

public class exceptionhandler {      @autowired     private sender sender;      public void handle(exception exception) throws exception {       if (!someexception.class.isassignablefrom(exception.getclass())) {         sender.sendmessage(exceptionutils.getstacktrace(exception));       }       throw exception;     } } 

here test code:

@mock private sender sender;  @injectmocks private exceptionhandler handler;  @test public void testhandler() throws exception {     someexception someexception = new someexception();      try {         handler.handle(someexception);     } catch (someexception thrownresult) {         assertequals(someexception, thrownresult);     }      verify(sender, times(1)).sendmessage(mockito.anystring()); } 

i need verify code called (on mocked object) after exception thrown, verification being ignored.

this not true, line is executed:

verify(sender, times(1)).sendmessage(mockito.anystring()); 

but fails verification error:

wanted not invoked: sender.sendmessage();

<...>

actually, there 0 interactions mock.

as expected, method never invoked, because condition !someexception.class.isassignablefrom(exception.getclass()) not satisfied - invoke handler.handle instance of someexception.


Comments

Popular posts from this blog

aws api gateway - SerializationException in posting new Records via Dynamodb Proxy Service in API -

asp.net - Problems sending emails from forum -