如何给Spring3 MVC中的Action做JUnit单元测试?

开发 架构
使用了spring3 MVC后,给action做单元测试也很方便,我以前从来不给action写单元测试的,再在不同了,方便了,所以一定要写。

使用了spring3 MVC后,给action做单元测试也很方便,我以前从来不给action写单元测试的,再在不同了,方便了,所以一定要写。

 

JUnitActionBase类是所有JUnit的测试类的父类

package test;    
import javax.servlet.http.HttpServletRequest;    
import javax.servlet.http.HttpServletResponse;    
import org.junit.BeforeClass;    
import org.springframework.mock.web.MockServletContext;    
import org.springframework.web.context.WebApplicationContext;    
import org.springframework.web.context.support.XmlWebApplicationContext;    
import org.springframework.web.servlet.HandlerAdapter;    
import org.springframework.web.servlet.HandlerExecutionChain;    
import org.springframework.web.servlet.HandlerMapping;    
import org.springframework.web.servlet.ModelAndView;    
import org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter;    
import org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping;    
/**    
* 说明: JUnit测试action时使用的基类   
*    
* @author  赵磊   
* @version 创建时间:2011-2-2 下午10:27:03     
*/     
public class JUnitActionBase {    
    private static HandlerMapping handlerMapping;    
    private static HandlerAdapter handlerAdapter;    
    /**   
     * 读取spring3 MVC配置文件   
     */    
    @BeforeClass    
 public static void setUp() {    
        if (handlerMapping == null) {    
            String[] configs = { "file:src/springConfig/springMVC.xml" };    
            XmlWebApplicationContext context = new XmlWebApplicationContext();    
            context.setConfigLocations(configs);    
            MockServletContext msc = new MockServletContext();    
            context.setServletContext(msc);         context.refresh();    
            msc.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, context);    
            handlerMapping = (HandlerMapping) context    
                    .getBean(DefaultAnnotationHandlerMapping.class);    
            handlerAdapter = (HandlerAdapter) context.getBean(context.getBeanNamesForType(AnnotationMethodHandlerAdapter.class)[0]);       
        }    
    }    
    
    /**   
     * 执行request对象请求的action   
     *    
     * @param request   
     * @param response   
     * @return   
     * @throws Exception   
     */    
    public ModelAndView excuteAction(HttpServletRequest request, HttpServletResponse response)    
 throws Exception {    
        HandlerExecutionChain chain = handlerMapping.getHandler(request);    
        final ModelAndView model = handlerAdapter.handle(request, response,    
                chain.getHandler());    
        return model;    
    }    
}    
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.

更多关系Spring的信息

Spring 论坛  http://www.itchm.com/forum-59-1.html

这是个JUnit测试类,我们可以new Request对象,来参与测试,太方便了。给request指定访问的URL,就可以请求目标Action了。

package test.com.app.user;    
import org.junit.Assert;    
import org.junit.Test;    
import org.springframework.mock.web.MockHttpServletRequest;    
import org.springframework.mock.web.MockHttpServletResponse;    
import org.springframework.web.servlet.ModelAndView;    
    
import test.JUnitActionBase;    
    
/**    
* 说明: 测试OrderAction的例子   
*    
* @author  赵磊    
* @version 创建时间:2011-2-2 下午10:26:55     
*/     
    
public class TestOrderAction extends JUnitActionBase {    
    @Test    
    public void testAdd() throws Exception {    
    MockHttpServletRequest request = new MockHttpServletRequest();    
        MockHttpServletResponse response = new MockHttpServletResponse();    
        request.setRequestURI("/order/add");    
        request.addParameter("id""1002");    
        request.addParameter("date""2010-12-30");    
        request.setMethod("POST");    
        // 执行URI对应的action    
        final ModelAndView mav = this.excuteAction(request, response);    
        // Assert logic    
        Assert.assertEquals("order/add", mav.getViewName());    
        String msg=(String)request.getAttribute("msg");    
        System.out.println(msg);    
    }    
}    
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.

需要说明一下 :由于当前最想版本的Spring(Test) 3.0.5还不支持@ContextConfiguration的注解式context file注入,所以还需要写个setUp处理下,否则类似于Tiles的加载过程会有错误,因为没有ServletContext。3.1的版本应该有更好的解决方案。

原文链接:http://www.cnblogs.com/ajuanabc/archive/2012/07/21/2601867.html

责任编辑:林师授 来源: 博客园
相关推荐

2011-08-11 13:02:43

Struts2Junit

2017-01-14 23:26:17

单元测试JUnit测试

2017-01-16 12:12:29

单元测试JUnit

2021-01-07 14:06:30

Spring BootJUnit5Java

2021-08-26 11:00:54

Spring BootJUnit5Java

2022-05-09 08:55:52

ORMMockGo

2011-11-18 15:18:41

Junit单元测试Java

2019-12-18 10:25:12

机器学习单元测试神经网络

2021-09-18 15:40:03

Vue单元测试命令

2012-05-17 09:09:05

Titanium单元测试

2009-07-24 11:33:12

MVC单元测试ASP.NET

2009-06-08 20:04:06

EclipseJUnit4单元测试

2021-04-23 07:33:10

SpringSecurity单元

2013-06-04 09:49:04

Spring单元测试软件测试

2017-01-14 23:42:49

单元测试框架软件测试

2020-07-21 14:40:45

Spring Boot单元测试Java

2012-02-07 09:08:50

Feed4JUnitJava

2021-09-01 12:03:49

Spring单元测试

2011-04-18 13:20:40

单元测试软件测试

2020-09-30 08:08:15

单元测试应用
点赞
收藏

51CTO技术栈公众号