Reputation: 61
I am testing a method with an expected exception. I also need to verify that some code was called (on a mocked object) after the exception is thrown, but verification is being ignored. Here is the 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 is the 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());
}
Upvotes: 0
Views: 1060
Reputation:
I also need to verify that some code was called (on a mocked object) after the exception is thrown, but verification is being ignored.
This is not true, this line is actually executed:
verify(sender, times(1)).sendMessage(Mockito.anyString());
But it fails verification with this error:
Wanted but not invoked: sender.sendMessage();
<...>
Actually, there were zero interactions with this mock.
As expected, that method was never invoked, because condition !SomeException.class.isAssignableFrom(exception.getClass())
was not satisfied - you invoke handler.handle
with instance of SomeException
.
Upvotes: 1