Spring MVC实现Junit Case
2024-08-29 15:50:49
Spring MVC中编写单元测试(WEB项目):
1. 首先开发一个基类,用于载入配置文件。以下所有的测试实现类都要继承这个类
- package com.yusj.basecase;
- import org.junit.runner.RunWith;
- import org.springframework.test.context.ContextConfiguration;
- import org.springframework.transaction.annotation.Transactional;
- import org.springframework.test.context.junit4.AbstractTransactionalJUnit4SpringContextTests;
- import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
- /**
- * 配置文件载入类
- * @ClassName: BaseSpringTestCase
- * @Description: 要想实现Spring自动注入,必须继承此类
- * @author yusj
- * @date 2014年6月9日 下午3:16:44
- *
- */
- @RunWith(SpringJUnit4ClassRunner.class)
- @ContextConfiguration({
- "file:src/main/webapp/WEB-INF/config/applicationContext.xml",
- "file:src/main/webapp/WEB-INF/config/captcha-context.xml",
- "file:src/main/webapp/WEB-INF/config/springmvc-servlet.xml"
- })
- // 添加注释@Transactional 回滚对数据库操作
- @Transactional
- public class BaseSpringTestCase {
- }
用户登录测试方法UserControllerTest如下:
- package com.yusj.web.controller;
- import static org.junit.Assert.assertEquals;
- import static org.junit.Assert.fail;
- import java.sql.SQLException;
- import org.junit.Before;
- import org.junit.Ignore;
- import org.junit.Test;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.mock.web.MockHttpServletRequest;
- import org.springframework.mock.web.MockHttpServletResponse;
- import com.sim.tracker.basecase.BaseSpringTestCase;
- /**
- *
- * @ClassName: UserControllerTest
- * @Description: 测试用户控制类
- * @author yusj
- * @date 2014年5月18日
- *
- */
- public class UserControllerTest extends BaseSpringTestCase {
- // 模拟request,response
- private MockHttpServletRequest request;
- private MockHttpServletResponse response;
- // 注入userController
- @Autowired
- private UserController userController ;
- // 执行测试方法之前初始化模拟request,response
- @Before
- public void setUp(){
- request = new MockHttpServletRequest();
- request.setCharacterEncoding("UTF-8");
- response = new MockHttpServletResponse();
- }
- /**
- *
- * @Title:testLogin
- * @Description: 测试用户登录
- * @author yusj
- * @date 2014年5月18日
- */
- @Test
- public void testLogin() {
- String username= "aaaa" ;
- String password = "bbbb" ;
- try {
- assertEquals("loginok",userController.login(username, password, request)) ;
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- }
注意:如果是Maven项目,当执行Maven install时,可能会报错误,造成不能正确生成war包。此时需要在pom.xml中加入如下配置:
- <project>
- <build>
- <plugins>
- <plugin>
- <groupId>org.apache.maven.plugins</groupId>
- <artifactId>maven-surefire-plugin</artifactId>
- <configuration>
- <testFailureIgnore>true</testFailureIgnore>
- </configuration>
- </plugin>
- </plugins>
- </build>
- </project>
注意:因为模拟request,response需要javax.servlet,AsycnContext类的支持,所以还需要导入javax.servlet3.0 Jar包的支持。
maven pom.xml配置代码如下:
- <dependencies>
- <dependency>
- <groupId>javax.servlet</groupId>
- <artifactId>javax.servlet-api</artifactId>
- <version>3.1.0</version>
- </dependency>
- </dependencies>
可以到http://www.mvnrepository.com/中输入关键字javax.servlet搜索下载。下载方式见我的另一篇文章:http://ysj5125094.iteye.com/blog/2082097
最新文章
- gdb 基本知识
- Windows Azure Virtual Machine (24) Azure VM支持多网卡功能
- C++浅析——继承类中构造和析构顺序
- 学习CSS3BUTTON(一)
- volley(5) 参数total_remain:totalqty, data:[{ bar_status:XX , bar_code: ";XX";,bar_remain:XX, bar_whcode:";XX"; , bar_prodcode:";XX";,bar_id:XX,bar_location: ";XX";, pr_detail: ";XX"; , bar_batchcode:method:POST
- UI进阶 科大讯飞(2) 语音合成(文字转换成语音)
- jQuery+css+div--一些细节详解
- MEF 编程指南(二):定义可组合部件和契约
- Sql Server 函数的操作实例!(返回一条Select语句查询后的临时表)
- CI脚本异常退出问题定位
- 全新的.NET解释器 - Mono已经到来
- APNS IOS 消息推送
- 1.5 下载和安装VMWare
- python 知识梳理
- 09、高级编程之基于排序机制的wordcount程序
- 利用JavaFx开发RIA桌面应用-事件监听
- 微信小程序编译包的获取与解压——在手机中获取小程序编译包wxapkg
- CONE NAT 和 Symmetric NAT
- 本地Maven库添加SQLServer2012 sqljdbc4.jar
- 导出WPS office文档格式的说明
热门文章
- winform 使用 ReportViewer做报表
- linux c网络编程之函数
- 连接linux数据库Oracle时报错ORA-12541: TNS: 无监听程序
- Facebook 网络模拟工具 ATC部署及使用
- oracle执行计划之-表连接方式
- 【WEB前端】CSS继承性和层叠性(极度重要)
- 关于vue.js中列表渲染练习
- 关于Unity中SteamVR_Controller.Input的错误
- 详解wait和waitpid函数
- [leetcode] 根据String数组构造TreeNode,用于LeetCode树结构相关的测试用例