Spring MVC的单元测试

来源:互联网 发布:三星软件更新 编辑:程序博客网 时间:2024/06/12 01:22

功能代码按照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 {}  
 


统计覆盖率。单元测试的标准往往是代码附带率,发现比较好的工具是CodePro Analytix,它是google收购的一个项目,项目主页:https://developers.google.com/java-dev-tools/codepro/doc/
这个工具的功能都很实用,它还可以自动生成测试代码。测试代码以独立的项目存在,可以根据功能代码的流程分支生成的测试方法,比如功能代码里有一个if else,测试代码就会生成3个测试方法,以便每个分支都能覆盖到。
我们主要使用它的代码覆盖率功能,这个工具不但可以统计测试时的代码覆盖率,还可以统计debug时的覆盖率。
1、按照安装说明,将工具安装进Eclipse
2、右键点击项目,选择CodePro Tools --> Instrument for Code Coverage。
再运行测试类或debug,都会出覆盖率的统计。

但是统计覆盖率会降低代码运行效率,所以,不需要统计时再 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. }  
 
0 0
原创粉丝点击