Spring MVC实现Junit Case
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
Spring MVC实现Junit Case的更多相关文章
- spring mvc 和junit 4集成的注意点
常规步骤: 1.导入jar包,主要有两个,spring-test 和 junit4,主要用maven管理,直接依赖即可.可以在这个网站上进行查找或下载:http://mvnrepository.com ...
- Spring mvc中junit测试遇到com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException错误怎么解决
今天遇到图片中的错误,纠结了一下,弄清楚了怎么从控制台中读取错误信息,并修改错误. com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: ...
- Spring MVC 项目搭建 -2- 添加service ,dao,junit
Spring MVC 项目搭建 -2- 添加service ,dao,junit 1.dao public class Hero { private String name; public Strin ...
- JUnit+Mockito结合测试Spring MVC Controller
[本文出自天外归云的博客园] 概要简述 利用JUnit结合Mockito,再加上spingframework自带的一些方法,就可以组合起来对Spring MVC中的Controller层进行测试. 在 ...
- spring Mvc + Mybatis 中使用junit
在Spring Mvc + Mybatis的项目中我们有时候需要在测试代码中注入Dao操作数据库,对表进行增删改查,实现如下: 这是一般的maven项目项目结构 测试代码一般写在src/test/ja ...
- 基于Spring + Spring MVC + Mybatis + shiro 高性能web构建
一直想写这篇文章,前段时间 痴迷于JavaScript.NodeJs.AngularJS,做了大量的研究,对前后端交互有了更深层次的认识. 今天抽个时间写这篇文章,我有预感,这将是一篇很详细的文章,详 ...
- Spring MVC 学习总结(一)——MVC概要与环境配置
一.MVC概要 MVC是模型(Model).视图(View).控制器(Controller)的简写,是一种软件设计规范,用一种将业务逻辑.数据.显示分离的方法组织代码,MVC主要作用是降低了视图与业务 ...
- [转]基于Spring + Spring MVC + Mybatis 高性能web构建
http://blog.csdn.net/zoutongyuan/article/details/41379851/ 一直想写这篇文章,前段时间 痴迷于JavaScript.NodeJs.Angula ...
- 基于Spring + Spring MVC + Mybatis 高性能web构建
基于Spring + Spring MVC + Mybatis 高性能web构建 一直想写这篇文章,前段时间 痴迷于JavaScript.NodeJs.AngularJs,做了大量的研究,对前后端交互 ...
随机推荐
- eclipse中jre system library ,web app libraries,referenced libraries,user libraries
在eclipse ide中进行开发时,为了方面jar的管理用了user libraries,但是遇到了一些问题,对于其中jre system library ,web app libraries,re ...
- EM界面 ORA-12505: TNS: 监听程序当前无法识别连接描述符中所给出的 SID (DBD ERROR: OCIServerAttach)
我的是10g,打开EM,另外都正常,就有这个问题到实例的代理连接 状态 失败 详细资料 ORA-12505: TNS: 监听程序当前无法识别连接描述符中所给出的 SID (DBD ERROR: OCI ...
- Matlab编程基础
平台:Win7 64 bit,Matlab R2014a(8.3) “Matlab”是“Matrix Laboratory” 的缩写,中文“矩阵实验室”,是强大的数学工具.本文侧重于Matlab的编程 ...
- HashMap归档-超越昨天的自己系列
java HashMap 读一下源码,一个数组存储数据: transient Entry[] table; 内部存key和value的内部类: static class Entry<K,V> ...
- SQL---Chapter01 数据库和SQL
数据库类型: 层次数据库(Hierarchical Database, HDB) 数据通过层次结构(树形结构)的方式表示出来. 关系型数据库(Relational Database, RDB) 使用专 ...
- Mark Down绘图语法
以下语法在网易云笔记中测试通过. 绘图的标志位是三个单引号``` 开始 ``` 结尾 ,注意是英文半角的单引号,以下的字符也是英文半角状态下的才正确. 搜狗输入法的要特别注意,记得把shift 切 ...
- 备受SQL青睐的“1”
写在前面:所用sql语句皆是在oracle 11g r1 数据库中实验. 在sql书写中,经常会用到数字1,例如 count(1),select 1, where 1=1等等,这样做有何好处呢?下面我 ...
- 【转】oracle in和exists、not in和not exists原理和性能探究
转自http://www.2cto.com/database/201310/251176.html 对于in和exists.not in和not exists还是有很多的人有疑惑,更有甚者禁用not ...
- React的虚拟DOM
ReactJs的一大特点就是引进了虚拟dom(Virtual DOM)的概念.为什么我们需要Virtual DOM,Virtual DOM给我们带来了什么优势. 首先我们要了解一下浏览器的工作流. 当 ...
- CodeForces 239 Long Path
每个房间有两个单向出口,就是只能进不能出,这个开始理解错了. 进入房间的时候,首先要在屋顶画一个叉叉,如果画完之后叉叉的个数是奇数的话:那么就从第二条出口出去,会到达p[ i ]房间:如果叉叉的个数是 ...