Unhandled IOException when instantiating ResponseException for test

Tags: , ,



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.

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);
  }
}


Source: stackoverflow