### Question:
RuntimeExceptionFilter implements Filter { @Override public Promise<Response, NeverThrowsException> filter(Context context, Request request, Handler next) { final PromiseImpl<Response, NeverThrowsException> promise = PromiseImpl.create(); try { next.handle(context, request) .thenOnResult(new ResultHandler<Response>() { @Override public void handleResult(Response result) { promise.handleResult(result); } }) .thenOnRuntimeException(onRuntimeException(promise)); } catch (RuntimeException exception) { onRuntimeException(promise).handleRuntimeException(exception); } return promise; }  @Override Promise<Response, NeverThrowsException> filter(Context context, Request request, Handler next);  }### Answer:
@Test public void shouldReturnTheSameResponse() throws Exception { final Response response = new Response(Status.OK); Handler next = new ResponseHandler(response); RuntimeExceptionFilter filter = new RuntimeExceptionFilter(); Promise<Response, NeverThrowsException> promise = filter.filter(new RootContext(), new Request(), next); assertThat(promise.get()).isSameAs(response); }
@Test public void shouldReturnErrorResponseWhenResponseCompletedWithRuntimeException() throws Exception { final RuntimeException exception = new RuntimeException("Boom"); Handler next = new Handler() { @Override public Promise<Response, NeverThrowsException> handle(Context context, Request request) { return runtimeExceptionPromise(exception); } }; RuntimeExceptionFilter filter = new RuntimeExceptionFilter(); Promise<Response, NeverThrowsException> promise = filter.filter(new RootContext(), new Request(), next); final Response response = promise.get(); assertThat(response.getStatus()).isEqualTo(Status.INTERNAL_SERVER_ERROR); assertThat(response.getCause()).isSameAs(exception); }
@Test public void shouldReturnErrorResponseWhenHandlerThrowsRuntimeException() throws Exception { final RuntimeException exception = new RuntimeException("Boom"); Handler next = new Handler() { @Override public Promise<Response, NeverThrowsException> handle(Context context, Request request) { throw exception; } }; RuntimeExceptionFilter filter = new RuntimeExceptionFilter(); Promise<Response, NeverThrowsException> promise = filter.filter(new RootContext(), new Request(), next); final Response response = promise.get(); assertThat(response.getStatus()).isEqualTo(Status.INTERNAL_SERVER_ERROR); assertThat(response.getCause()).isSameAs(exception); }