Skip to content
Advertisement

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.

Advertisement

Answer

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);
  }
}
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement