如何对Spring MVC注释控制器进行单元测试?
2022-09-02 05:38:10
我正在学习Spring 2.5教程,同时尝试将代码/设置更新到Spring 3.0。
在春季2.5中,我有HelloController(供参考):
public class HelloController implements Controller {
protected final Log logger = LogFactory.getLog(getClass());
public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
logger.info("Returning hello view");
return new ModelAndView("hello.jsp");
}
}
以及 HelloController 的 JUnit 测试(供参考):
public class HelloControllerTests extends TestCase {
public void testHandleRequestView() throws Exception{
HelloController controller = new HelloController();
ModelAndView modelAndView = controller.handleRequest(null, null);
assertEquals("hello", modelAndView.getViewName());
}
}
但是现在我将控制器更新到Spring 3.0,它现在使用注释(我还添加了一条消息):
@Controller
public class HelloController {
protected final Log logger = LogFactory.getLog(getClass());
@RequestMapping("/hello")
public ModelAndView handleRequest() {
logger.info("Returning hello view");
return new ModelAndView("hello", "message", "THIS IS A MESSAGE");
}
}
知道我正在使用JUnit 4.9,有人可以解释一下如何对最后一个控制器进行单元测试吗?