In Spring MVC 3.0, how do I test that a particular method gets invoked?
For example, I have this controller method:
public class myController {
@RequestMapping(value = "/create", method = RequestMethod.GET)
public ModelAndView create(ModelMap map) {
map.put("category", new Category());
return new ModelAndView("views/someView", map);
}
}
How do I test that this create() method gets called when somebody requests http://example.com/create url.
In Unit Tests, you should only test your controller’s Java code, without using any Servlet technology.
In integration tests you can do one of several things:
Use the
org.springframework.mock.webpackage in the spring-test artifact, which contains Mock Objects for request, response, servletContext to fire fake requests at your controllers and read the data from the fake responses.Or use a web testing framework like Selenium that works against a deployed webapp.