How to verify that exception was thrown by mock method in code below?
It simply throw exception on checking method without ending verify.
// import mockito
...
@Test
public void someTest() throws Exception {
// reset and setup mock
reset(mock);
when(mock.getObj(Matchers.anyLong()))
.thenReturn(new Obj());
when(mock.save(any(Obj.class)))
.thenThrow(new RuntimeException("Error!"));
// work where mock is used (it throws no exceptions)
work();
// verify that exception on mock.save() was thrown
// ! PROBLEM HERE: exception throws right here and verify doesn't end
verify(mock, times(1)).save(any(Obj.class));
}
UPD
work() - only sends message to Kafka-consumer (which is being tested) which works on embedded Kafka-server.
mock - mocks some object in consumer logic.
In this case, checking out the exception is an ADDITIONAL check for checking a certain branch of the consumer algorithm (other asserts not important (deleted): they checks that message was worked).