HttpExceptionUtils { public static Response createJerseyExceptionResponse(Response.Status status, Throwable ex) { Map<String, Object> json = new LinkedHashMap<String, Object>(); json.put(ERROR_MESSAGE_JSON, getOneLineMessage(ex)); json.put(ERROR_EXCEPTION_JSON, ex.getClass().getSimpleName()); json.put(ERROR_CLASSNAME_JSON, ex.getClass().getName()); Map<String, Object> response = Collections.singletonMap(ERROR_JSON, json); return Response.status(status).type(MediaType.APPLICATION_JSON). entity(response).build(); }  static void createServletExceptionResponse(
      HttpServletResponse response, int status, Throwable ex); static Response createJerseyExceptionResponse(Response.Status status,
      Throwable ex); @SuppressWarnings("unchecked") static void validateResponse(HttpURLConnection conn,
      int expectedStatus); static final String ERROR_JSON; static final String ERROR_EXCEPTION_JSON; static final String ERROR_CLASSNAME_JSON; static final String ERROR_MESSAGE_JSON; }
@Test public void testCreateJerseyException() throws IOException { Exception ex = new IOException("Hello IOEX"); Response response = HttpExceptionUtils.createJerseyExceptionResponse( Response.Status.INTERNAL_SERVER_ERROR, ex); Assert.assertEquals(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), response.getStatus()); Assert.assertArrayEquals( Arrays.asList(MediaType.APPLICATION_JSON_TYPE).toArray(), response.getMetadata().get("Content-Type").toArray()); Map entity = (Map) response.getEntity(); entity = (Map) entity.get(HttpExceptionUtils.ERROR_JSON); Assert.assertEquals(IOException.class.getName(), entity.get(HttpExceptionUtils.ERROR_CLASSNAME_JSON)); Assert.assertEquals(IOException.class.getSimpleName(), entity.get(HttpExceptionUtils.ERROR_EXCEPTION_JSON)); Assert.assertEquals("Hello IOEX", entity.get(HttpExceptionUtils.ERROR_MESSAGE_JSON)); }