如何給Spring3 MVC中的Action做JUnit單元測(cè)試?
使用了spring3 MVC后,給action做單元測(cè)試也很方便,我以前從來不給action寫單元測(cè)試的,再在不同了,方便了,所以一定要寫。
JUnitActionBase類是所有JUnit的測(cè)試類的父類
- 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測(cè)試action時(shí)使用的基類
- *
- * @author 趙磊
- * @version 創(chuàng)建時(shí)間: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]);
- }
- }
- /**
- * 執(zhí)行request對(duì)象請(qǐng)求的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;
- }
- }
更多關(guān)系Spring的信息
Spring 論壇 http://www.itchm.com/forum-59-1.html
這是個(gè)JUnit測(cè)試類,我們可以new Request對(duì)象,來參與測(cè)試,太方便了。給request指定訪問的URL,就可以請(qǐng)求目標(biāo)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;
- /**
- * 說明: 測(cè)試OrderAction的例子
- *
- * @author 趙磊
- * @version 創(chuàng)建時(shí)間: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");
- // 執(zhí)行URI對(duì)應(yīng)的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);
- }
- }
需要說明一下 :由于當(dāng)前最想版本的Spring(Test) 3.0.5還不支持@ContextConfiguration的注解式context file注入,所以還需要寫個(gè)setUp處理下,否則類似于Tiles的加載過程會(huì)有錯(cuò)誤,因?yàn)闆]有ServletContext。3.1的版本應(yīng)該有更好的解決方案。
原文鏈接:http://www.cnblogs.com/ajuanabc/archive/2012/07/21/2601867.html