如何对 Spring MVC 带注释的控制器进行单元测试?
我正在遵循 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,有人可以解释一下如何对此进行单元测试吗最后一个控制器?
I am following a Spring 2.5 tutorial and trying, at the same time, updating the code/setup to Spring 3.0.
In Spring 2.5 I had the HelloController (for reference):
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");
}
}
And a JUnit test for the HelloController (for reference):
public class HelloControllerTests extends TestCase {
public void testHandleRequestView() throws Exception{
HelloController controller = new HelloController();
ModelAndView modelAndView = controller.handleRequest(null, null);
assertEquals("hello", modelAndView.getViewName());
}
}
But now I updated the controller to Spring 3.0, and it now uses annotations (I also added a message):
@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");
}
}
Knowing that I am using JUnit 4.9, can some one explain me how to unit test this last controller?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
基于注释的 Spring MVC 的一个优点是可以以简单的方式测试它们,如下所示:
这种方法有什么问题吗?
对于更高级的集成测试,有一个 Spring 文档中的引用 到 org.springframework.mock.web< /a>.
One advantage of annotation-based Spring MVC is that they can be tested in a straightforward manner, like so:
Is there any problem with this approach?
For more advanced integration testing, there is a reference in Spring documentation to the org.springframework.mock.web.
使用 mvc:annotation-driven ,您必须执行 2 个步骤:首先使用 HandlerMapping 解析对处理程序的请求,然后您可以通过 HandlerAdapter 使用该处理程序执行该方法。类似于:
这适用于 Spring 3.1,但我想每个版本都必须存在一些变体。看看 Spring 3.0 代码,我想说 DefaultAnnotationHandlerMapping 和 AnnotationMethodHandlerAdapter 应该可以解决问题。
With mvc:annotation-driven you have to have 2 steps: first you resolve the request to handler using HandlerMapping, then you can execute the method using that handler via HandlerAdapter. Something like:
This works with Spring 3.1, but I guess some variant of this must exist for every version. Looking at the Spring 3.0 code, I'd say DefaultAnnotationHandlerMapping and AnnotationMethodHandlerAdapter should do the trick.
您还可以查看其他独立于 Spring 的 Web 测试框架,例如 HtmlUnit 或 硒。除了 Sasha 所描述的之外,您不会发现仅使用 JUnit 更强大的策略,除非您绝对应该断言该模型。
You can also look into other web testing frameworks that are independent of Spring like HtmlUnit, or Selenium. You won't find any more robust strategy with JUnit alone other than what Sasha has described, except you should definitely assert the model.