diff --git a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/servlet/error/BasicErrorControllerIntegrationTests.java b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/servlet/error/BasicErrorControllerIntegrationTests.java index e9e971a99c..21bc9b5965 100755 --- a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/servlet/error/BasicErrorControllerIntegrationTests.java +++ b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/servlet/error/BasicErrorControllerIntegrationTests.java @@ -38,7 +38,6 @@ import org.junit.After; import org.junit.AfterClass; import org.junit.BeforeClass; import org.junit.Test; -import org.junit.runner.RunWith; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration; @@ -47,8 +46,6 @@ import org.springframework.boot.autoconfigure.http.HttpMessageConvertersAutoConf import org.springframework.boot.autoconfigure.web.servlet.DispatcherServletAutoConfiguration; import org.springframework.boot.autoconfigure.web.servlet.ServletWebServerFactoryAutoConfiguration; import org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration; -import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; import org.springframework.boot.test.web.client.TestRestTemplate; import org.springframework.context.ConfigurableApplicationContext; import org.springframework.context.annotation.Bean; @@ -58,8 +55,6 @@ import org.springframework.http.HttpStatus; import org.springframework.http.MediaType; import org.springframework.http.RequestEntity; import org.springframework.http.ResponseEntity; -import org.springframework.test.annotation.DirtiesContext; -import org.springframework.test.context.junit4.SpringRunner; import org.springframework.test.util.ReflectionTestUtils; import org.springframework.validation.BindException; import org.springframework.web.bind.MethodArgumentNotValidException; @@ -80,9 +75,6 @@ import static org.assertj.core.api.Assertions.assertThat; * @author Dave Syer * @author Stephane Nicoll */ -@RunWith(SpringRunner.class) -@DirtiesContext -@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT) public class BasicErrorControllerIntegrationTests { private ConfigurableApplicationContext context; @@ -109,14 +101,15 @@ public class BasicErrorControllerIntegrationTests { ResponseEntity entity = new TestRestTemplate() .getForEntity(createUrl("?trace=true"), Map.class); assertErrorAttributes(entity.getBody(), "500", "Internal Server Error", - IllegalStateException.class, "Expected!", "/"); + null, "Expected!", "/"); assertThat(entity.getBody().containsKey("trace")).isFalse(); } @Test @SuppressWarnings("rawtypes") public void testErrorForMachineClientTraceParamStacktrace() throws Exception { - load("--server.error.include-stacktrace=on-trace-param"); + load("--server.error.include-exception=true", + "--server.error.include-stacktrace=on-trace-param"); ResponseEntity entity = new TestRestTemplate() .getForEntity(createUrl("?trace=true"), Map.class); assertErrorAttributes(entity.getBody(), "500", "Internal Server Error", @@ -131,7 +124,7 @@ public class BasicErrorControllerIntegrationTests { ResponseEntity entity = new TestRestTemplate() .getForEntity(createUrl("?trace=true"), Map.class); assertErrorAttributes(entity.getBody(), "500", "Internal Server Error", - IllegalStateException.class, "Expected!", "/"); + null, "Expected!", "/"); assertThat(entity.getBody().containsKey("trace")).isFalse(); } @@ -142,14 +135,14 @@ public class BasicErrorControllerIntegrationTests { ResponseEntity entity = new TestRestTemplate() .getForEntity(createUrl("?trace=false"), Map.class); assertErrorAttributes(entity.getBody(), "500", "Internal Server Error", - IllegalStateException.class, "Expected!", "/"); + null, "Expected!", "/"); assertThat(entity.getBody().containsKey("trace")).isTrue(); } @Test @SuppressWarnings("rawtypes") public void testErrorForAnnotatedException() throws Exception { - load(); + load("--server.error.include-exception=true"); ResponseEntity entity = new TestRestTemplate() .getForEntity(createUrl("/annotated"), Map.class); assertErrorAttributes(entity.getBody(), "400", "Bad Request", @@ -160,7 +153,7 @@ public class BasicErrorControllerIntegrationTests { @Test @SuppressWarnings("rawtypes") public void testErrorForAnnotatedNoReasonException() throws Exception { - load(); + load("--server.error.include-exception=true"); ResponseEntity entity = new TestRestTemplate() .getForEntity(createUrl("/annotatedNoReason"), Map.class); assertErrorAttributes(entity.getBody(), "406", "Not Acceptable", @@ -171,7 +164,7 @@ public class BasicErrorControllerIntegrationTests { @Test @SuppressWarnings("rawtypes") public void testBindingExceptionForMachineClient() throws Exception { - load(); + load("--server.error.include-exception=true"); RequestEntity request = RequestEntity.get(URI.create(createUrl("/bind"))) .accept(MediaType.APPLICATION_JSON).build(); ResponseEntity entity = new TestRestTemplate().exchange(request, Map.class); @@ -185,7 +178,7 @@ public class BasicErrorControllerIntegrationTests { @Test @SuppressWarnings("rawtypes") public void testRequestBodyValidationForMachineClient() throws Exception { - load(); + load("--server.error.include-exception=true"); RequestEntity request = RequestEntity .post(URI.create(createUrl("/bodyValidation"))) .contentType(MediaType.APPLICATION_JSON).body("{}"); @@ -197,6 +190,17 @@ public class BasicErrorControllerIntegrationTests { assertThat(resp).contains(MethodArgumentNotValidException.class.getName()); } + @Test + @SuppressWarnings("rawtypes") + public void testNoExceptionByDefaultForMachineClient() throws Exception { + load(); + RequestEntity request = RequestEntity.get(URI.create(createUrl("/bind"))) + .accept(MediaType.APPLICATION_JSON).build(); + ResponseEntity entity = new TestRestTemplate().exchange(request, Map.class); + String resp = entity.getBody().toString(); + assertThat(resp).doesNotContain("org.springframework.validation.BindException"); + } + @Test public void testConventionTemplateMapping() throws Exception { load(); @@ -212,8 +216,14 @@ public class BasicErrorControllerIntegrationTests { Class exception, String message, String path) { assertThat(content.get("status")).as("Wrong status").isEqualTo(status); assertThat(content.get("error")).as("Wrong error").isEqualTo(error); - assertThat(content.get("exception")).as("Wrong exception") - .isEqualTo(exception.getName()); + if (exception != null) { + assertThat(content.get("exception")).as("Wrong exception") + .isEqualTo(exception.getName()); + } + else { + assertThat(content.containsKey("exception")) + .as("Exception attribute should not be set").isFalse(); + } assertThat(content.get("message")).as("Wrong message").isEqualTo(message); assertThat(content.get("path")).as("Wrong path").isEqualTo(path); } @@ -262,7 +272,7 @@ public class BasicErrorControllerIntegrationTests { @Override protected void renderMergedOutputModel(Map model, HttpServletRequest request, HttpServletResponse response) - throws Exception { + throws Exception { response.getWriter().write("ERROR_BEAN"); } };