Reputation: 33
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
Upvotes: 3
Views: 3693
Reputation: 280102
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.
Upvotes: 7