ErrorMessage { @Override public String toString() { JSONObject output = new JSONObject(); output.put("status", status); output.put("error", getErrorAsJson()); return output.toString(2); } ErrorMessage(Exception exception, int status); @Override String toString();  }
@Test public void testToString() { ErrorMessage errorMessage = new ErrorMessage(new IllegalStateException("illegal state"), SERVICE_UNAVAILABLE.getStatus()); assertEquals("{\n" + " \"error\": {\n" + " \"reason\": \"There was internal problem at backend\",\n" + " \"details\": \"illegal state\",\n" + " \"type\": \"IllegalStateException\"\n" + " },\n" + " \"status\": 503\n" + "}", errorMessage.toString()); }
@Test public void testBadRequestToString() { ErrorMessage errorMessage = new ErrorMessage(new IllegalStateException(), BAD_REQUEST.getStatus()); assertEquals("{\n" + " \"error\": {\n" + " \"reason\": \"Invalid Query\",\n" + " \"details\": \"\",\n" + " \"type\": \"IllegalStateException\"\n" + " },\n" + " \"status\": 400\n" + "}", errorMessage.toString()); }
@Test public void testToStringWithEmptyErrorMessage() { ErrorMessage errorMessage = new ErrorMessage(new IllegalStateException(), SERVICE_UNAVAILABLE.getStatus()); assertEquals("{\n" + " \"error\": {\n" + " \"reason\": \"There was internal problem at backend\",\n" + " \"details\": \"\",\n" + " \"type\": \"IllegalStateException\"\n" + " },\n" + " \"status\": 503\n" + "}", errorMessage.toString()); }