成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

如何給Spring3 MVC中的Action做JUnit單元測試?

開發 架構
使用了spring3 MVC后,給action做單元測試也很方便,我以前從來不給action寫單元測試的,再在不同了,方便了,所以一定要寫。

使用了spring3 MVC后,給action做單元測試也很方便,我以前從來不給action寫單元測試的,再在不同了,方便了,所以一定要寫。

 

JUnitActionBase類是所有JUnit的測試類的父類

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

更多關系Spring的信息

Spring 論壇  http://www.itchm.com/forum-59-1.html

這是個JUnit測試類,我們可以new Request對象,來參與測試,太方便了。給request指定訪問的URL,就可以請求目標Action了。

  1. package test.com.app.user;    
  2. import org.junit.Assert;    
  3. import org.junit.Test;    
  4. import org.springframework.mock.web.MockHttpServletRequest;    
  5. import org.springframework.mock.web.MockHttpServletResponse;    
  6. import org.springframework.web.servlet.ModelAndView;    
  7.     
  8. import test.JUnitActionBase;    
  9.     
  10. /**    
  11. * 說明: 測試OrderAction的例子   
  12. *    
  13. * @author  趙磊    
  14. * @version 創建時間:2011-2-2 下午10:26:55     
  15. */     
  16.     
  17. public class TestOrderAction extends JUnitActionBase {    
  18.     @Test    
  19.     public void testAdd() throws Exception {    
  20.     MockHttpServletRequest request = new MockHttpServletRequest();    
  21.         MockHttpServletResponse response = new MockHttpServletResponse();    
  22.         request.setRequestURI("/order/add");    
  23.         request.addParameter("id""1002");    
  24.         request.addParameter("date""2010-12-30");    
  25.         request.setMethod("POST");    
  26.         // 執行URI對應的action    
  27.         final ModelAndView mav = this.excuteAction(request, response);    
  28.         // Assert logic    
  29.         Assert.assertEquals("order/add", mav.getViewName());    
  30.         String msg=(String)request.getAttribute("msg");    
  31.         System.out.println(msg);    
  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

2011-11-18 15:18:41

Junit單元測試Java

2022-05-09 08:55:52

ORMMockGo

2021-09-18 15:40:03

Vue單元測試命令

2019-12-18 10:25:12

機器學習單元測試神經網絡

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

單元測試框架軟件測試

2012-02-07 09:08:50

Feed4JUnitJava

2020-07-21 14:40:45

Spring Boot單元測試Java

2021-09-01 12:03:49

Spring單元測試

2011-04-18 13:20:40

單元測試軟件測試

2020-09-30 08:08:15

單元測試應用
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 国产精品一区二区久久 | 在线超碰| 九九精品在线 | 午夜影院普通用户体验区 | 黑人巨大精品欧美一区二区一视频 | 免费久久99精品国产婷婷六月 | 999久久久| 婷婷激情综合 | 成人av免费播放 | 国产一区二区三区久久久久久久久 | 无码日韩精品一区二区免费 | 国产欧美精品区一区二区三区 | 色永久| 天天看天天爽 | 99精品久久久 | 国产精品一区一区三区 | 黄色av免费 | av黄色在线观看 | 日日躁狠狠躁aaaaxxxx | 国产精品爱久久久久久久 | 久久久蜜桃一区二区人 | 欧美黄在线观看 | 亚洲免费高清 | 久久精品视频免费观看 | 亚洲aⅴ | 成人午夜看片 | 国产一区二区影院 | 久久精品国产精品青草 | 在线中文字幕视频 | 天天操天天干天天透 | 色欧美片视频在线观看 | 在线观看亚洲 | 91麻豆精品国产91久久久久久久久 | 美女一区| 男女羞羞免费视频 | 久久国产精品-久久精品 | 国产精品区一区二 | 亚洲精品v日韩精品 | 日韩综合在线 | 国产一级片免费看 | 色欧美片视频在线观看 |