0

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()); } 

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.