Search code examples
javaspring-testspring-restcontrollerspring-test-mvcspring-mvc-test

Spring MVC RestController test failed to find valid mapping


I failed to test RestController method with path variable

INFO org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping - Mapped URL path [/my/{env}] onto handler 'myController'
INFO org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping - Mapped URL path [/my/{env}.*] onto handler 'myController'
INFO org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping - Mapped URL path [/my/{env}/] onto handler 'myController'
INFO org.springframework.test.web.servlet.TestDispatcherServlet - FrameworkServlet '': initialization completed in 205 ms
WARN org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/my/dev/auth] in DispatcherServlet with name ''

My Test

@ContextConfiguration( classes = {Config.class, MyController.class})
@ActiveProfiles(profiles= {"dev"})
@WebAppConfiguration
public class MyControllerTest extends AbstractTestNGSpringContextTests {

    @Autowired
    private WebApplicationContext wac;

    private MockMvc mockMvc;


    @BeforeClass
    public void setup() {
        mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build();
    }

    @Test
    public void testAuth() throws Exception {
        MockHttpSession httpSession =  new MockHttpSession(wac.getServletContext(), UUID.randomUUID().toString());
        MockHttpServletRequestBuilder mockHttpServletRequestBuilder = MockMvcRequestBuilders
        .post("/my/dev/auth").content("<XML></XML>")
        .session(httpSession)
        .contentType(MediaType.APPLICATION_XML);
         MvcResult mvcResult = this.mockMvc.perform(mockHttpServletRequestBuilder)
                  .andDo(MockMvcResultHandlers.print())
                  .andReturn(); 

My Controller

@RestController
@RequestMapping(value = "/my/{env}")
public class MyController {

@PostMapping(value = "auth", consumes = MediaType.APPLICATION_XML_VALUE, produces = MediaType.APPLICATION_XML_VALUE)
    public @ResponseBody ResponseEntity<Response> authenticate(
            @PathVariable("env") String gameEnvironment, @RequestBody String xml,
            HttpServletRequest httpRequest) {

EDIT

Removing path variable result in similar results

[main] INFO org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping - Mapped URL path [/my] onto handler 'myController'
[main] INFO org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping - Mapped URL path [/my.*] onto handler 'myController'
[main] INFO org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping - Mapped URL path [/my/] onto handler 'myController'
[main] WARN org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/my/auth] in DispatcherServlet with name ''

Solution

  • Test was missing classes for full loading of relevant configuration

    @ContextConfiguration( classes = {Config.class, MyController.class
        ,OtherConfig.class})