### Question:
XMLResourceExceptionHandler implements ResponseWriter { @Override public void write(MessageContext context, AuthenticationException exception) { Reject.ifNull(exception); try { ResourceException jre; if (exception instanceof AuthenticationFailedException) { jre = new PermanentException(Status.UNAUTHORIZED.getCode(), exception.getMessage(), null); } else if (exception.getCause() instanceof ResourceException) { jre = (ResourceException) exception.getCause(); } else { LOGGER.error(exception.getMessage(), exception); jre = new InternalServerErrorException("Authentication Failed", exception); } AuditTrail auditTrail = context.getAuditTrail(); List<Map<String, Object>> failureReasonList = auditTrail.getFailureReasons(); if (failureReasonList != null && !failureReasonList.isEmpty()) { jre.setDetail(json(object(field("failureReasons", failureReasonList)))); } Response response = context.getResponse(); response.setStatus(Status.valueOf(jre.getCode())); context.<Response>getResponse().getHeaders().put(ContentTypeHeader.valueOf(MediaType.XML_UTF_8.toString())); ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); Transformer transformer = XMLUtils.getTransformerFactory().newTransformer(); transformer.transform(new DOMSource(asXMLDOM(jre.includeCauseInJsonValue().toJsonValue().asMap())), new StreamResult(outputStream)); response.getEntity().setBytes(outputStream.toByteArray()); } catch (TransformerException e1) { throw new IllegalStateException("Could not write XML to response", e1); } }  Collection<MediaType> handles(); @Override void write(MessageContext context, AuthenticationException exception); static Document asXMLDOM(Map<String, Object> map);  }### Answer:
@Test public void testWrite() throws Exception { MessageContext context = mock(MessageContext.class); AuditTrail mockAudit = mock(AuditTrail.class); Response response = new Response(); doReturn(mockAudit).when(context).getAuditTrail(); doReturn(response).when(context).getResponse(); String message = "I don't know where it is"; ResourceException ex = new NotFoundException(message); AuthenticationException ex2 = new AuthenticationException(ex); handler.write(context, ex2); assertThat(response.getStatus()).isEqualTo(Status.NOT_FOUND); String text = response.getEntity().getString(); assertThat(text).contains("<message>" + message + "</message>"); assertThat(text).contains("<code>404</code>"); }