Reputation: 313
Trying to create a unit test for an elastic search exception handler that uses ResponseException but having trouble setting up the object. Mocking doesn't work as ResponseException is a final class.
private ResponseException responseException = new ResponseException(response);
produces the following compilation error: Unhandled exception: java.io.IOException
Any help is appreciated.
Upvotes: 0
Views: 128
Reputation: 1670
Adding mockito-inline dependency will enable mockito to mock final classes.
'org.mockito:mockito-inline:2.15.0'
Upvotes: 0
Reputation: 5591
Typical Java trick for this case:
private ResponseException responseException = create(response);
private ResponseException create(Response response) {
try {
return new ResponseException(response);
} catch(Exception ex) {
throw new RuntimeException(ex);
}
}
Upvotes: 2