I have the following service and test methods and I am trying the code execute catch (ApplicationException e) { }
block.
public abstract class ApplicationException extends RuntimeException {
// ....
}
public void create(Request request) {
try {
// ...
} catch (ApplicationException e) {
// I want the code hits this block and check the values in here
}
}
Here is the test method:
@InjectMocks
private ProductServiceImpl productService;
@Test
public void test() {
// variable defnitions and stubbings (code omitted)
willAnswer( invocation -> { throw new RuntimeException("abc msg"); })
.given(productService).create(request);
// or
// doThrow(new RuntimeException()).when(productService).create(request);
// service method call
productService.create(Request request);
}
When I debug the code, I get error on doThrow
line:
org.mockito.exceptions.misusing.NotAMockException: Argument passed to when() is not a mock!
So, how can I solve the problem?