Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I had similar problem when trying to test <code>ExceptionHandler</code> annotated with <code>@ControllerAdvice</code>. In my case I had to add <code>@Configuration</code> file with <code>@EnableWebMvc</code> annotation to <code>@ContextConfiguration</code> on test class.</p> <p>So my test looked like this:</p> <pre><code>@RunWith(SpringJUnit4ClassRunner.class) @WebAppConfiguration @ContextConfiguration(classes = { RestProcessingExceptionHandler.class, TestConfiguration.class, RestProcessingExceptionThrowingController.class }) public class TestRestProcessingExceptionHandler { private MockMvc mockMvc; @Autowired WebApplicationContext wac; @Before public void setup() { mockMvc = webAppContextSetup(wac).build(); } @Configuration // !!! this is very important - conf with this annotation // must be included in @ContextConfiguration @EnableWebMvc public static class TestConfiguration { } @Controller @RequestMapping("/tests") public static class RestProcessingExceptionThrowingController { @RequestMapping(value = "/exception", method = GET) public @ResponseBody String find() { throw new RestProcessingException("global_error_test"); } } @Test public void testHandleException() throws Exception { mockMvc.perform(get("/tests/exception")) .andExpect(new ResultMatcher() { @Override public void match(MvcResult result) throws Exception { result.getResponse().getContentAsString().contains("global_error_test"); } }) .andExpect(status().isBadRequest()); } } </code></pre> <p>With <code>@EnableWebMvc</code> configuration my test passed.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload