### Question:
UmaExceptionHandler { protected void handleException(Response response, Throwable throwable) { Throwable cause = throwable.getCause(); if (cause instanceof UmaException) { UmaException exception = (UmaException) cause; setExceptionResponse(response, cause, exception.getStatusCode(), exception.getError(), exception.getDetail()); } else if (cause instanceof OAuth2Exception) { OAuth2Exception exception = (OAuth2Exception) cause; setExceptionResponse(response, cause, exception.getStatusCode(), exception.getError(), null); } else { setExceptionResponse(response, throwable, response.getStatus().getCode(), "server_error", null); } } @Inject  UmaExceptionHandler(JacksonRepresentationFactory jacksonRepresentationFactory);   }### Answer:
@Test @SuppressWarnings("unchecked") public void shouldSet500ExceptionResponse() throws IOException { Response response = mock(Response.class); Exception exception = new Exception("MESSAGE"); Status status = new Status(444, exception); given(response.getStatus()).willReturn(status); exceptionFilter.handleException(response, exception); ArgumentCaptor<JacksonRepresentation> exceptionResponseCaptor = ArgumentCaptor.forClass(JacksonRepresentation.class); verify(response).setEntity(exceptionResponseCaptor.capture()); Map<String, String> responseBody = (Map<String, String>) exceptionResponseCaptor.getValue().getObject(); assertThat(responseBody).containsOnly(entry("error", "server_error"), entry("error_description", "MESSAGE")); ArgumentCaptor<Status> statusCaptor = ArgumentCaptor.forClass(Status.class); verify(response).setStatus(statusCaptor.capture()); assertThat(statusCaptor.getValue().getCode()).isEqualTo(444); assertThat(statusCaptor.getValue().getThrowable()).isEqualTo(exception); }
@Test @SuppressWarnings("unchecked") public void shouldSetUmaExceptionResponse() throws IOException { Response response = mock(Response.class); Throwable throwable = mock(Throwable.class); Exception exception = new UmaException(444, "ERROR", "DESCRIPTION"); given(throwable.getCause()).willReturn(exception); Status status = new Status(444, exception); given(response.getStatus()).willReturn(status); exceptionFilter.handleException(response, throwable); ArgumentCaptor<JacksonRepresentation> exceptionResponseCaptor = ArgumentCaptor.forClass(JacksonRepresentation.class); verify(response).setEntity(exceptionResponseCaptor.capture()); Map<String, String> responseBody = (Map<String, String>) exceptionResponseCaptor.getValue().getObject(); assertThat(responseBody).containsOnly(entry("error", "ERROR"), entry("error_description", "DESCRIPTION")); ArgumentCaptor<Status> statusCaptor = ArgumentCaptor.forClass(Status.class); verify(response).setStatus(statusCaptor.capture()); assertThat(statusCaptor.getValue().getCode()).isEqualTo(444); assertThat(statusCaptor.getValue().getThrowable()).isEqualTo(exception); }
@Test @SuppressWarnings("unchecked") public void shouldSetUmaExceptionResponseWithDetail() throws IOException { Response response = mock(Response.class); Throwable throwable = mock(Throwable.class); Exception exception = new UmaException(444, "ERROR", "DESCRIPTION") .setDetail(json(object(field("DETAIL", "VALUE")))); given(throwable.getCause()).willReturn(exception); Status status = new Status(444, exception); given(response.getStatus()).willReturn(status); exceptionFilter.handleException(response, throwable); ArgumentCaptor<JacksonRepresentation> exceptionResponseCaptor = ArgumentCaptor.forClass(JacksonRepresentation.class); verify(response).setEntity(exceptionResponseCaptor.capture()); Map<String, String> responseBody = (Map<String, String>) exceptionResponseCaptor.getValue().getObject(); assertThat(responseBody).containsOnly(entry("error", "ERROR"), entry("error_description", "DESCRIPTION"), entry("DETAIL", "VALUE")); ArgumentCaptor<Status> statusCaptor = ArgumentCaptor.forClass(Status.class); verify(response).setStatus(statusCaptor.capture()); assertThat(statusCaptor.getValue().getCode()).isEqualTo(444); assertThat(statusCaptor.getValue().getThrowable()).isEqualTo(exception); }