5

We are building a project using spring boot (v1.3.3.RELEASE), we have a custom ErrorController which look like this

@RestController
@RequestMapping("/error")
public class GlobalErrorController extends AbstractErrorController {
    @Autowired
    public GlobalErrorController(ErrorAttributes errorAttributes) {
        super(errorAttributes);
    }

    @Override
    public String getErrorPath() {
        return "/error";
    }

    @RequestMapping(produces = MediaType.ALL_VALUE)
    public ResponseEntity error(HttpServletRequest request) {
        HttpStatus status = getStatus(request);
        if (status.is5xxServerError()) {
            //log maybe
        }
        return new ResponseEntity(status);
    }
}

We find it worked well when we start the spring boot application, and try to access it through curl or browser, it just responds the status code and no body(content).

But when we tried to write an integration test for this controller, we found out that the ErrorController will never be called, some sample code lies here:

@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = {MyApplication.class, TestConfiguration.class})
@WebAppConfiguration
public class GlobalErrorControllerIntegrationTest {
    private MockMvc mockMvc;

    @Autowired
    private WebApplicationContext wac;

    @Before
    public void setUp() throws Exception {
        mockMvc = MockMvcBuilders.webAppContextSetup(wac).build();

    }

    @Test
    public void shouldRespondWithServerErrorButNoContent() throws Exception {
        mockMvc.perform(get("/test/err")).andExpect(status().is5xxServerError()).andExpect(content().string(""));
    }

}

MyApplication is the entry of the application which is annotated with @SpringBootApplication, and TestConfiguration is some test fixtures which will be used to help run the test, it looks like this:

 @Configuration
 @ComponentScan("com.my.test.fixtures")
 public class TestConfiguration {
 }

and also we have a test controller which will raise an error

@RestController
public class TestErrorController {
    @RequestMapping("/test/err")
    public int raiseError() {
        return 1 / 0;
    }
}

Can somebody help to figure out whats the problem?

Is this caused due to no embedded server when using spring TestContext, If so, is there any better solution?

Thanks advance.

kevinjom
  • 343
  • 2
  • 15
  • @luboskrnac `AbstractErrorController ` comes from `spring-boot` – kevinjom Apr 19 '16 at 01:06
  • I'm afraid I don't have a good solution for you at the moment, but you might want to track [this issue](https://github.com/spring-projects/spring-boot/issues/5574) for future updates. – Phil Webb Apr 21 '16 at 17:37

0 Answers0