小男孩‘自慰网亚洲一区二区,亚洲一级在线播放毛片,亚洲中文字幕av每天更新,黄aⅴ永久免费无码,91成人午夜在线精品,色网站免费在线观看,亚洲欧洲wwwww在线观看

分享

Spring MVC的單元測試

 taitanewtopqjh 2015-01-31

功能代碼按照Spring的標準書寫,無論是通過XML配置還是通過annotation配置都可以。

 

測試代碼最重要的是告訴framework去哪里找bean的配置。

 

以Dao的Test為例:

 

 

Java代碼  收藏代碼
  1. //告訴framework怎么運行這個類  
  2. @RunWith(SpringJUnit4ClassRunner.class)   
  3. //bean的配置文件路徑,這個是Test類的classpath路徑,如果是Spring推薦的目錄結構,應該在:項目目錄/src/test/resources/里  
  4. @ContextConfiguration(locations = "classpath:app-config.xml")    
  5. public class TestPatchDao extends AbstractTransactionalJUnit4SpringContextTests {  
  6.     //取得要測試的Dao類  
  7.     @Resource  
  8.     private PatchDao patchDao;  
  9.   
  10.     @Before  
  11.     public void setUp() throws Exception {  
  12.     }  
  13.   
  14.     @After  
  15.     public void tearDown() throws Exception {  
  16.     }  
  17.   
  18.     /** 
  19.      * 測試方法 
  20.      */  
  21.     @Test  
  22.     public void testGetPatchList_1() {  
  23.         //Dao的某個方法  
  24.         List<Map<String, Object>> list = patchDao.getPatchList(1"00C8002D00000000"1);  
  25.         assertEquals(1, list.size());  
  26.     }  
  27. }  
 

 

再舉一個Controller的例子

 

Java代碼  收藏代碼
  1. @RunWith(SpringJUnit4ClassRunner.class)  
  2. @ContextConfiguration(locations = {"classpath:app-config.xml""classpath:mvc-config.xml"})  
  3. public class TestMainCtrl extends AbstractTransactionalJUnit4SpringContextTests {  
  4.     @Autowired  
  5.     private MainCtrl controller;  
  6.   
  7.     //這種方法適用于Springframework3.0,3.1換了handler的處理類。  
  8.     @Autowired  
  9.     private AnnotationMethodHandlerAdapter handlerAdapter;  
  10.   
  11.     private final MockHttpServletRequest request = new MockHttpServletRequest();  
  12.     private final MockHttpServletResponse response = new MockHttpServletResponse();  
  13.   
  14.     @Test  
  15.     public void testMain4User() throws Exception {  
  16.         request.setRequestURI("/main");  
  17.         request.setMethod(HttpMethod.POST.name());  
  18.         HttpSession session = request.getSession();  
  19.         //設置 認證信息  
  20.         session.setAttribute(CommonConstants.SESSION_USER_TYPE, 1);  
  21.         session.setAttribute(CommonConstants.SESSION_USER_ID, 0);  
  22.         session.setAttribute(CommonConstants.SESSION_USER_ACC, "aa1");  
  23.   
  24.         ModelAndView mav = handlerAdapter.handle(request, response, controller);  
  25.         assertEquals("/regist", mav.getViewName());  
  26.     }  
  27. }  
 

TestSuite的寫法,將Test類用注解的方式配置到TestSuite類上。

Java代碼  收藏代碼
  1. import org.junit.runner.RunWith;  
  2. import org.junit.runners.Suite;  
  3. import org.junit.runners.Suite.SuiteClasses;  
  4.   
  5. @RunWith(Suite.class)  
  6. @SuiteClasses( { TestPatchDao.class, TestMainCtrl.class })  
  7. public class TestSuite {}  
 


統(tǒng)計覆蓋率。單元測試的標準往往是代碼附帶率,發(fā)現比較好的工具是CodePro Analytix,它是google收購的一個項目,項目主頁:https://developers.google.com/java-dev-tools/codepro/doc/
這個工具的功能都很實用,它還可以自動生成測試代碼。測試代碼以獨立的項目存在,可以根據功能代碼的流程分支生成的測試方法,比如功能代碼里有一個if else,測試代碼就會生成3個測試方法,以便每個分支都能覆蓋到。
我們主要使用它的代碼覆蓋率功能,這個工具不但可以統(tǒng)計測試時的代碼覆蓋率,還可以統(tǒng)計debug時的覆蓋率。
1、按照安裝說明,將工具安裝進Eclipse
2、右鍵點擊項目,選擇CodePro Tools --> Instrument for Code Coverage。
再運行測試類或debug,都會出覆蓋率的統(tǒng)計。

但是統(tǒng)計覆蓋率會降低代碼運行效率,所以,不需要統(tǒng)計時再 Unistrument 就可以了。

---------------------------------------------

Springframework3.1和springsecure的controller測試類例子:


Java代碼  收藏代碼
  1. @RunWith(SpringJUnit4ClassRunner.class)  
  2. @ContextConfiguration(locations = {"classpath:root-context.xml",   
  3.         "classpath:servlet-context.xml""classpath:security-app-context.xml"})  
  4. public class TestSecureCtrl extends AbstractTransactionalJUnit4SpringContextTests {  
  5.     @Autowired  
  6.     private SecureCtrl controller;  
  7.     @Autowired  
  8.     private RequestMappingHandlerAdapter handlerAdapter;  
  9.   
  10.     private final MockHttpServletRequest request = new MockHttpServletRequest();  
  11.     private final MockHttpServletResponse response = new MockHttpServletResponse();  
  12.       
  13.     @Test  
  14.     public void testMain4User() throws Exception {  
  15.         request.setRequestURI("/secure");  
  16.         request.setMethod(HttpMethod.POST.name());  
  17.         HttpSession session = request.getSession();  
  18.           
  19.         //設置springsecure的內容  
  20.         List<GrantedAuthority> ga = new ArrayList<GrantedAuthority>();  
  21.         ga.add(new GrantedAuthorityImpl("ROLE_ALL"));  
  22.         User user = new User("account""password"truetruetruetrue, ga);  
  23.         SecurityContextHolder.getContext().setAuthentication(  
  24.         new UsernamePasswordAuthenticationToken(user, null));  
  25.         session.setAttribute(HttpSessionSecurityContextRepository.SPRING_SECURITY_CONTEXT_KEY, SecurityContextHolder.getContext());  
  26.   
  27.         ModelAndView mav = handlerAdapter.handle(request, response, new HandlerMethod(controller, "home", Model.class, HttpServletRequest.class));  
  28.   
  29.         assertEquals("home", mav.getViewName());  
  30.     }  
  31. }  
 

    本站是提供個人知識管理的網絡存儲空間,所有內容均由用戶發(fā)布,不代表本站觀點。請注意甄別內容中的聯(lián)系方式、誘導購買等信息,謹防詐騙。如發(fā)現有害或侵權內容,請點擊一鍵舉報。
    轉藏 分享 獻花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多