|
18 | 18 |
|
19 | 19 | import java.util.Map;
|
20 | 20 |
|
| 21 | +import javax.servlet.http.HttpServletRequest; |
| 22 | +import javax.servlet.http.HttpServletResponse; |
| 23 | + |
21 | 24 | import org.junit.Test;
|
22 | 25 | import org.junit.runner.RunWith;
|
23 | 26 | import org.springframework.beans.factory.annotation.Value;
|
| 27 | +import org.springframework.boot.SpringApplication; |
| 28 | +import org.springframework.boot.autoconfigure.web.BasicErrorControllerMockMvcTests.MinimalWebConfiguration; |
| 29 | +import org.springframework.boot.autoconfigure.web.BasicErrorControllerMockMvcTests.TestConfiguration; |
24 | 30 | import org.springframework.boot.test.IntegrationTest;
|
25 | 31 | import org.springframework.boot.test.SpringApplicationConfiguration;
|
26 | 32 | import org.springframework.boot.test.TestRestTemplate;
|
| 33 | +import org.springframework.context.annotation.Bean; |
| 34 | +import org.springframework.context.annotation.Configuration; |
27 | 35 | import org.springframework.http.ResponseEntity;
|
| 36 | +import org.springframework.test.annotation.DirtiesContext; |
28 | 37 | import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
29 | 38 | import org.springframework.test.context.web.WebAppConfiguration;
|
| 39 | +import org.springframework.validation.BindException; |
| 40 | +import org.springframework.web.bind.annotation.RequestMapping; |
| 41 | +import org.springframework.web.bind.annotation.RestController; |
| 42 | +import org.springframework.web.servlet.View; |
| 43 | +import org.springframework.web.servlet.view.AbstractView; |
30 | 44 |
|
31 | 45 | import static org.hamcrest.Matchers.containsString;
|
32 | 46 | import static org.hamcrest.Matchers.endsWith;
|
|
40 | 54 | * @author Dave Syer
|
41 | 55 | */
|
42 | 56 | @RunWith(SpringJUnit4ClassRunner.class)
|
43 |
| -@SpringApplicationConfiguration(classes = BasicErrorControllerMockMvcTests.TestConfiguration.class) |
| 57 | +@SpringApplicationConfiguration(classes = TestConfiguration.class) |
44 | 58 | @WebAppConfiguration
|
| 59 | +@DirtiesContext |
45 | 60 | @IntegrationTest("server.port=0")
|
46 | 61 | public class BasicErrorControllerIntegrationTest {
|
47 | 62 |
|
@@ -69,4 +84,49 @@ public void testBindingExceptionForMachineClient() throws Exception {
|
69 | 84 | assertThat(resp, containsString("errors=[{codes="));
|
70 | 85 | assertThat(resp, containsString("org.springframework.validation.BindException"));
|
71 | 86 | }
|
| 87 | + |
| 88 | + @Configuration |
| 89 | + @MinimalWebConfiguration |
| 90 | + public static class TestConfiguration { |
| 91 | + |
| 92 | + // For manual testing |
| 93 | + public static void main(String[] args) { |
| 94 | + SpringApplication.run(TestConfiguration.class, args); |
| 95 | + } |
| 96 | + |
| 97 | + @Bean |
| 98 | + public View error() { |
| 99 | + return new AbstractView() { |
| 100 | + @Override |
| 101 | + protected void renderMergedOutputModel(Map<String, Object> model, |
| 102 | + HttpServletRequest request, HttpServletResponse response) |
| 103 | + throws Exception { |
| 104 | + response.getWriter().write("ERROR_BEAN"); |
| 105 | + } |
| 106 | + }; |
| 107 | + } |
| 108 | + |
| 109 | + @RestController |
| 110 | + protected static class Errors { |
| 111 | + |
| 112 | + public String getFoo() { |
| 113 | + return "foo"; |
| 114 | + } |
| 115 | + |
| 116 | + @RequestMapping("/") |
| 117 | + public String home() { |
| 118 | + throw new IllegalStateException("Expected!"); |
| 119 | + } |
| 120 | + |
| 121 | + @RequestMapping("/bind") |
| 122 | + public String bind() throws Exception { |
| 123 | + BindException error = new BindException(this, "test"); |
| 124 | + error.rejectValue("foo", "bar.error"); |
| 125 | + throw error; |
| 126 | + } |
| 127 | + |
| 128 | + } |
| 129 | + |
| 130 | + } |
| 131 | + |
72 | 132 | }
|
0 commit comments