Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MockMvc reach the @Controller class although servlet mapping filtering

I'm new to Spring and this is my first question in Stack Overflow, my english also is basic...

I'm currently following the http://spring.io/guides/tutorials/web/3/ and I don't understand why the MvcMock can perform a get("/bbb") and reach my controller (mapped to @RequestMapping("/bbb")) even the AbstractAnnotationConfigDispatcherServletInitializer restrict the DispatcherSevlet mappings to new String[] { "/aaa" };?

Here is the code involved...

The Spring WebAppInitializer

public class WebAppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {

@Override
protected Class<?>[] getRootConfigClasses() {
    return new Class<?>[] { PersistenceConfig.class, CoreConfig.class };
}

@Override
protected Class<?>[] getServletConfigClasses() {
    return new Class<?>[] { WebConfig.class };
}

@Override
protected String[] getServletMappings() {
    return new String[] { "/aaa" };
}

@Override
protected Filter[] getServletFilters() {

    CharacterEncodingFilter characterEncodingFilter = new CharacterEncodingFilter();
    characterEncodingFilter.setEncoding("UTF-8");
    return new Filter[] { characterEncodingFilter };
}
}

The Spring controller

@Controller
@RequestMapping("/bbb")
public class SiteController {

private static final Logger LOG = LoggerFactory.getLogger(SiteController.class);

@Autowired
private MenuService menuService;

@RequestMapping(method = RequestMethod.GET)
@ResponseBody
public String getCurrentMenu() {
    ...
}

And the JUnit test that passes

@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@ContextConfiguration(classes = { PersistenceConfig.class, CoreConfig.class, WebConfig.class })
public class WebDomainIntegrationTest {

private static final String STANDARD = "Yummy Noodles";
private static final String CHEF_SPECIAL = "Special Yummy Noodles";
private static final String LOW_CAL = "Low cal Yummy Noodles";

private MockMvc mockMvc;

@Autowired
WebApplicationContext webApplicationContext;

@Before
public void setup() {
    mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build();
}

@Test
public void thatTextReturned() throws Exception {
            mockMvc.perform(get("/bbb")).andDo(print()).andExpect(content().string(containsString(STANDARD)))
            .andExpect(content().string(containsString(CHEF_SPECIAL))).andExpect(content().string(containsString(LOW_CAL)));

}

}

Thank's a lot for your help!

Laurent

like image 970
Laurent Bauchau Avatar asked Dec 21 '25 09:12

Laurent Bauchau


1 Answers

The answer is that your MockMvc configuration is not using your WebAppInitializer. This currently isn't supported by the Spring MVC test suite. Notice how you haven't registered anywhere. All you've done is to setup your context configuration with

@ContextConfiguration(classes = { PersistenceConfig.class, CoreConfig.class, WebConfig.class })

With that and

@WebAppConfiguration

MockMvc will register a DispatcherServlet with the @Controller classes in your context (and other things it looks for). It will then run the tests with that configuration. There is no /aaa in that configuration. All paths are absolute, as they are declared in your @Controller classes.

like image 79
Sotirios Delimanolis Avatar answered Dec 22 '25 23:12

Sotirios Delimanolis



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!