I have an error controller (to handle the path "/error") that works when the application is running, but when in an unit test with @WebMvcTest it does not work.
The error controller also works if I send a request direct to its path.
This is my error controller:
@Controller
public class ErrorController implements org.springframework.boot.web.servlet.error.ErrorController {
@RequestMapping("/error")
public ResponseEntity<List<Error>> handleError(HttpServletRequest request) {
and this is my test class:
@RunWith(SpringRunner.class)
@WebMvcTest({ErrorController.class})
public class ErrorControllerTest {
@Autowired
private MockMvc mockMvc;
I have also tried to add the class ErrorMvcAutoConfiguration in the WebMvcTest annotation.
I have debugged the ErrorMvcAutoConfiguration class and found that when the application is running it finds an error view resolver, but when running as unit test it does not find any.
The idea behind this test it to make sure the Spring configuration (which is code) that leads to the execution of this error controller is correct.