如何对 Spring MVC 注释控制器进行单元测试?

How to unit test a Spring MVC annotated controller?(如何对 Spring MVC 注释控制器进行单元测试?)
本文介绍了如何对 Spring MVC 注释控制器进行单元测试?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我正在关注 Spring 2.5 教程,同时尝试将代码/设置更新到 Spring 3.0.

I am following a Spring 2.5 tutorial and trying, at the same time, updating the code/setup to Spring 3.0.

Spring 2.5 我有 HelloController(供参考):

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");
    }
}

HelloController 的 JUnit 测试(供参考):

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());
    }
}

但现在我将控制器更新为 Spring 3.0,它现在使用注释(我还添加了一个 消息):

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");
    }
}

知道我使用的是 JUnit 4.9,有人能解释一下如何对最后一个控制器进行单元测试吗?

Knowing that I am using JUnit 4.9, can some one explain me how to unit test this last controller?

推荐答案

基于注解的 Spring MVC 的一个优点是可以直接测试它们,如下所示:

One advantage of annotation-based Spring MVC is that they can be tested in a straightforward manner, like so:

import org.junit.Test;
import org.junit.Assert;
import org.springframework.web.servlet.ModelAndView;

public class HelloControllerTest {
   @Test
   public void testHelloController() {
       HelloController c= new HelloController();
       ModelAndView mav= c.handleRequest();
       Assert.assertEquals("hello", mav.getViewName());
       ...
   }
}

这种方法有什么问题吗?

Is there any problem with this approach?

对于更高级的集成测试,有一个 Spring 文档中的参考 到 org.springframework.mock.web.

For more advanced integration testing, there is a reference in Spring documentation to the org.springframework.mock.web.

这篇关于如何对 Spring MVC 注释控制器进行单元测试?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

How to send data to COM PORT using JAVA?(如何使用 JAVA 向 COM PORT 发送数据?)
How to make a report page direction to change to quot;rtlquot;?(如何使报表页面方向更改为“rtl?)
Use cyrillic .properties file in eclipse project(在 Eclipse 项目中使用西里尔文 .properties 文件)
Is there any way to detect an RTL language in Java?(有没有办法在 Java 中检测 RTL 语言?)
How to load resource bundle messages from DB in Java?(如何在 Java 中从 DB 加载资源包消息?)
How do I change the default locale settings in Java to make them consistent?(如何更改 Java 中的默认语言环境设置以使其保持一致?)