我正在跟随一个Spring 2.5教程,同时尝试将代码/设置更新为Spring 3.0。
在 Spring 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,有人能够解释一下如何对这个最后一个控制器进行单元测试吗?