Jub10
Jub10

Reputation: 313

Unhandled IOException when instantiating ResponseException for test

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

Answers (2)

Kumar V
Kumar V

Reputation: 1670

Adding mockito-inline dependency will enable mockito to mock final classes.

'org.mockito:mockito-inline:2.15.0'

Upvotes: 0

Shadov
Shadov

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

Related Questions