Spring MVC实现Junit Case

Spring MVC中编写单元测试(WEB项目):

1. 首先开发一个基类,用于载入配置文件。以下所有的测试实现类都要继承这个类

Java代码  收藏代码
  1. package com.yusj.basecase;  
  2.   
  3. import org.junit.runner.RunWith;  
  4. import org.springframework.test.context.ContextConfiguration;  
  5. import org.springframework.transaction.annotation.Transactional;  
  6. import org.springframework.test.context.junit4.AbstractTransactionalJUnit4SpringContextTests;  
  7. import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;  
  8.   
  9. /** 
  10. * 配置文件载入类  
  11. * @ClassName: BaseSpringTestCase  
  12. * @Description: 要想实现Spring自动注入,必须继承此类 
  13. * @author yusj   
  14. * @date 2014年6月9日 下午3:16:44  
  15.  */  
  16. @RunWith(SpringJUnit4ClassRunner.class)  
  17. @ContextConfiguration({  
  18.     "file:src/main/webapp/WEB-INF/config/applicationContext.xml",  
  19.     "file:src/main/webapp/WEB-INF/config/captcha-context.xml",  
  20.     "file:src/main/webapp/WEB-INF/config/springmvc-servlet.xml"  
  21. })  
  22.   
  23. // 添加注释@Transactional 回滚对数据库操作    
  24. @Transactional  
  25. public class BaseSpringTestCase {  
  26.       
  27. }  

用户登录测试方法UserControllerTest如下:

Java代码  收藏代码
  1. package com.yusj.web.controller;  
  2.   
  3. import static org.junit.Assert.assertEquals;  
  4. import static org.junit.Assert.fail;  
  5.   
  6. import java.sql.SQLException;  
  7.   
  8. import org.junit.Before;  
  9. import org.junit.Ignore;  
  10. import org.junit.Test;  
  11. import org.springframework.beans.factory.annotation.Autowired;  
  12. import org.springframework.mock.web.MockHttpServletRequest;  
  13. import org.springframework.mock.web.MockHttpServletResponse;  
  14.   
  15. import com.sim.tracker.basecase.BaseSpringTestCase;  
  16.   
  17. /** 
  18.  *  
  19. * @ClassName: UserControllerTest  
  20. * @Description: 测试用户控制类   
  21. * @author yusj  
  22. * @date 2014年5月18日  
  23.  */  
  24. public class UserControllerTest extends BaseSpringTestCase {  
  25.   
  26.     // 模拟request,response  
  27.     private MockHttpServletRequest request;  
  28.     private MockHttpServletResponse response;   
  29.       
  30.     // 注入userController  
  31.     @Autowired  
  32.     private UserController userController ;  
  33.       
  34.     // 执行测试方法之前初始化模拟request,response  
  35.     @Before    
  36.     public void setUp(){    
  37.         request = new MockHttpServletRequest();      
  38.         request.setCharacterEncoding("UTF-8");      
  39.         response = new MockHttpServletResponse();      
  40.     }    
  41.       
  42.     /** 
  43.      *  
  44.      * @Title:testLogin 
  45.      * @Description: 测试用户登录   
  46.      * @author yusj    
  47.      * @date 2014年5月18日 
  48.      */  
  49.     @Test  
  50.     public void testLogin() {  
  51.         String username= "aaaa" ;  
  52.         String password = "bbbb" ;   
  53.         try {  
  54.             assertEquals("loginok",userController.login(username, password, request)) ;  
  55.         } catch (SQLException e) {  
  56.             e.printStackTrace();  
  57.         }  
  58.     }  
  59. }  

注意:如果是Maven项目,当执行Maven install时,可能会报错误,造成不能正确生成war包。此时需要在pom.xml中加入如下配置:

Xml代码  收藏代码
  1. <project>  
  2.     <build>  
  3.         <plugins>  
  4.             <plugin>  
  5.                 <groupId>org.apache.maven.plugins</groupId>  
  6.                 <artifactId>maven-surefire-plugin</artifactId>  
  7.                  <configuration>  
  8.                      <testFailureIgnore>true</testFailureIgnore>  
  9.                  </configuration>  
  10.             </plugin>  
  11.         </plugins>  
  12.     </build>  
  13. </project>  

注意:因为模拟request,response需要javax.servlet,AsycnContext类的支持,所以还需要导入javax.servlet3.0 Jar包的支持。

maven pom.xml配置代码如下:

Xml代码  收藏代码
  1. <dependencies>  
  2.          <dependency>  
  3.         <groupId>javax.servlet</groupId>  
  4.         <artifactId>javax.servlet-api</artifactId>  
  5.         <version>3.1.0</version>  
  6.     </dependency>       
  7. </dependencies>  

可以到http://www.mvnrepository.com/中输入关键字javax.servlet搜索下载。下载方式见我的另一篇文章:http://ysj5125094.iteye.com/blog/2082097

原文地址:https://www.cnblogs.com/ceshi2016/p/6247625.html