### Question:
AuthenticationServiceV1 { protected Response handleErrorResponse(Request request, Status status, Exception exception) { Reject.ifNull(status); Response response = new Response(status); final Map<String, Object> rep = new HashMap<>(); if (exception instanceof RestAuthResponseException) { final RestAuthResponseException authResponseException = (RestAuthResponseException) exception; for (Map.Entry<String, String> entry : authResponseException.getResponseHeaders().entrySet()) { response.getHeaders().put(entry.getKey(), entry.getValue()); } rep.putAll(authResponseException.getJsonResponse().asMap()); } else if (exception instanceof RestAuthException) { final RestAuthException authException = (RestAuthException)exception; if (authException.getFailureUrl() != null) { rep.put("failureUrl", authException.getFailureUrl()); } rep.put("errorMessage", getLocalizedMessage(request, exception)); } else if (exception == null) { rep.put("errorMessage", status.getReasonPhrase()); } else { rep.put("errorMessage", getLocalizedMessage(request, exception)); } response.setEntity(rep); return response; } @Inject  AuthenticationServiceV1(RestAuthenticationHandler restAuthenticationHandler); @Post Response authenticate(@Contextual Context context, @Contextual Request httpRequest);  }### Answer:
@Test public void shouldReturnUnauthorizedCodeWithJsonFailureMessage() throws IOException { Request httpRequest = new Request(); JsonValue jsonValue = json(object( field("failure", true), field("reason", "http-auth-failed"), field("authId", "12345"))); RestAuthResponseException exception = new RestAuthResponseException(RestAuthException.UNAUTHORIZED, Collections.<String, String>emptyMap(), jsonValue); Response response = authServiceV1.handleErrorResponse(httpRequest, Status.valueOf(exception.getStatusCode()), exception); assertThat(response.getStatus()).isEqualToComparingFieldByField(Status.UNAUTHORIZED); JsonValue responseBody = json(response.getEntity().getJson()); assertThat(responseBody).booleanAt("failure").isTrue(); assertThat(responseBody).stringAt("reason").isEqualTo("http-auth-failed"); assertThat(responseBody).stringAt("authId").isEqualTo("12345"); }
@Test public void shouldReturnUnauthorizedCodeWithJsonErrorMessage() throws IOException { Request httpRequest = new Request(); RestAuthException exception = new RestAuthException(401, "Invalid Password!!"); exception.setFailureUrl("http: Response response = authServiceV1.handleErrorResponse(httpRequest, Status.valueOf(401), exception); assertThat(response.getStatus()).isEqualTo(Status.UNAUTHORIZED); JsonValue responseBody = json(response.getEntity().getJson()); assertThat(responseBody).stringAt("errorMessage").isEqualTo("Invalid Password!!"); assertThat(responseBody).stringAt("failureUrl").isEqualTo("http: }