I'm using Spring Boot 1.2.5. I have some problem doing a unit test because of exception handling.
public class RestControllerTest {
//It is used in restController
@Mock
protected Service service;
@InjectMocks
protected RestController restController;
@Test
protected void testMe() throws Exception {
MockMvc mockMVC = MockMvcBuilders.standaloneSetup(restController) .build();
doThrow(new IOException()).when(service).methodCalled();
mockMvc.perform(post("/something")
.param("val", "test"))
.andExpect(status().isBadRequest());
}
}
I have a controlleradvice that has to handle the exceptions:
@ControllerAdvice
public class MyControllerAdvice {
@ResponseStatus(value = HttpStatus.CONFLICT)
// 409
@ExceptionHandler(IOException.class)
public void ioException(final IOException e) {
log.error("Request raised an IOException", e);
}
}
But clearly it is not used since I'm in standalone mode. How can I use it? Now thr test fail because the exception is thrown and not converted into the desired HttpStatus. I could add the annotated method in the class, thus solving the problem, but then I have to repeat this for every controller and I don't like code duplication.
Aucun commentaire:
Enregistrer un commentaire