From f39d4978c3a62cdbdc5403dde3bd41dbb163006c Mon Sep 17 00:00:00 2001 From: Dave Syer Date: Mon, 27 Oct 2014 15:06:26 +0000 Subject: [PATCH] Fix tests for JSON error rendering See gh-1762 --- .../web/DefaultErrorAttributes.java | 2 +- .../BasicErrorControllerIntegrationTests.java | 30 +++++++++++++++++-- 2 files changed, 29 insertions(+), 3 deletions(-) diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/DefaultErrorAttributes.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/DefaultErrorAttributes.java index 85ac4f85ba4d..2170c151b3b6 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/DefaultErrorAttributes.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/DefaultErrorAttributes.java @@ -122,7 +122,7 @@ private void addErrorDetails(Map errorAttributes, } } Object message = getAttribute(requestAttributes, "javax.servlet.error.message"); - if (message != null || errorAttributes.get("message") == null) { + if ((message != null || errorAttributes.get("message") == null) && !(error instanceof BindingResult)) { errorAttributes.put("message", message == null ? "No message available" : message); } diff --git a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/BasicErrorControllerIntegrationTests.java b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/BasicErrorControllerIntegrationTests.java index 8ad0e345d8e2..e09a88faa3ee 100644 --- a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/BasicErrorControllerIntegrationTests.java +++ b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/BasicErrorControllerIntegrationTests.java @@ -33,6 +33,7 @@ import org.springframework.boot.test.TestRestTemplate; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; +import org.springframework.http.HttpStatus; import org.springframework.http.MediaType; import org.springframework.http.RequestEntity; import org.springframework.http.ResponseEntity; @@ -41,6 +42,7 @@ import org.springframework.test.context.web.WebAppConfiguration; import org.springframework.validation.BindException; import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.ResponseStatus; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.servlet.View; import org.springframework.web.servlet.view.AbstractView; @@ -73,8 +75,21 @@ public void testErrorForMachineClient() throws Exception { "http://localhost:" + this.port, Map.class); assertThat(entity.getBody().toString(), endsWith("status=500, " + "error=Internal Server Error, " - + "exception=java.lang.IllegalStateException, " + "message=Expected!, " - + "path=/}")); + + "exception=java.lang.IllegalStateException, " + + "message=Server Error, " + "path=/}")); + } + + @Test + @SuppressWarnings("rawtypes") + public void testErrorForAnnotatedException() throws Exception { + ResponseEntity entity = new TestRestTemplate().getForEntity( + "http://localhost:" + this.port + "/annotated", Map.class); + assertThat( + entity.getBody().toString(), + endsWith("status=400, " + + "error=Bad Request, " + + "exception=org.springframework.boot.autoconfigure.web.BasicErrorControllerIntegrationTests$TestConfiguration$Errors$ExpectedException, " + + "message=Expected!, " + "path=/annotated}")); } @Test @@ -124,6 +139,11 @@ public String home() { throw new IllegalStateException("Expected!"); } + @RequestMapping("/annotated") + public String annotated() { + throw new ExpectedException(); + } + @RequestMapping("/bind") public String bind(HttpServletRequest request) throws Exception { BindException error = new BindException(this, "test"); @@ -131,6 +151,12 @@ public String bind(HttpServletRequest request) throws Exception { throw error; } + @ResponseStatus(value = HttpStatus.BAD_REQUEST, reason = "Expected!") + @SuppressWarnings("serial") + private static class ExpectedException extends RuntimeException { + + } + } }