SpringTest(一)
|
SpringMvcTest总结:
最近要做单元测试,所以选择的是SpringTest这个测试框架。
1.准备工作。(导入jar包)
因为使用Maven管理jar包,所以在要做单元测试的模块中的pom文件中加入如下代码:
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-oxm</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.5</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.version}</version>
</dependency>
当然你可以将上述代码写在父模块中,那么就不必每个子模块中都重复写入上面的代码。
2.编写测试类。
@RunWith
这是一个类注解,表明该测试类的执行类是什么。如果没有,测试执行类会是默认的。
常见的测试执行类:
(1)SpringJUnit4ClassRunner.class:
(2)Parameterized.class:参数化运行器,配合@Parameters使用junit的参数化功能
(3)JUnit4.class:junit4的默认运行器
(4)Suite.class,用法如下:
@RunWith(Suite.class)
@SuiteClasses({ATest.class,BTest.class,CTest.class})
当执行有上面标注的测试类,那么ATest.class,BTest.class,CTest.class等多个测试类也会执行。
@ContextConfiguration 注解,这是一个类级别的注解。
该注解有以下两个常用的属性:
locations:可以通过该属性手工指定 Spring 配置文件所在的位置,可以指定一个或多个 Spring 配置文件。如下所示:
@ContextConfiguration(locations={“xx/yy/beans1.xml”,” xx/yy/beans2.xml”})。如果没有设置该属性,
那么,会在默认位置找配置文件,默认配置文件为“测试类名-context.xml”
inheritLocations:是否要继承父测试用例类中的 Spring 配置文件,默认为 true。
3.运行测试。
因为本人搭建的是Maven项目,所以我会把每个模块的测试类放在对应模块的src/test目录下,那么当我执行"mvn clean test"的时候,
maven会自动执行src/test目录下的所有测试类中的测试用例。十分方便,不用逐个点击测试用例进行点击。
存在问题:
还没有对rest服务的测试进行研究,还有dao层,service层。所以还需要继续学习。
|
SpringTest(一)的更多相关文章
- Spring-test使用JUnit时,测试类autowired报错,create bean error
Spring-test使用JUnit时,测试类里面使用autowired会报错, 报create bean error...... 但是controller里面@autowired可以正常运行的. 在 ...
- 架构实例之SpringTest
架构实例之SpringTest 1.开发工具和开发环境 开发工具: MyEclipse10,JDK1.6.0_13(32位),Tomcat7.0(32位),mysql5.7.13 开发环境 ...
- spring-test测试demo
如果是maven项目,pom中增加如下依赖: <dependency> <groupId>org.springframework</groupId& ...
- SpringTest框架JUnit单元测试用例获取ApplicationContext实例的方法
步骤 1.继承AbstractJUnit4SpringContextTests 2.引入ApplicationContext 示例代码 1 2 3 4 5 6 7 8 9 10 11 12 13 14 ...
- 【转】SpringTest框架JUnit单元测试用例获取ApplicationContext实例的方法
转自:http://www.coderli.com/junit-spring-test-applicationcontext JUnit单元测试用例中使用Spring框架,直接方式如下. @RunWi ...
- 真正意义上的spring环境中的单元测试方案spring-test与mokito完美结合
真正意义上的spring环境中的单元测试方案spring-test与mokito完美结合 博客分类: java 测试 单元测试SpringCC++C# 一.要解决的问题: spring环境中 ...
- SpringTest 使用说明 -构建无污染纯绿色事务测试框架 (记录用)
@ContextConfiguration({"classpath:applicationContext.xml","classpath:spring/buyer/app ...
- 使用Spring的隐式注解和装配以及使用SpringTest框架
SpringTestConfiguration 1.加入jar 包spring-test-4.3.9.RELEASE.jar 2.写基本的Component 注意级联状态下 需要给需要调用的属性加入 ...
- junit+mock+spring-test构建后台单元测试
from:从0开始,构建前后端分离应用 1. 一些基本概念 1.1 为什么要进行单元测试?我自己的理解是 1.能够快速发现问题.避免衍生BUG的出现 在对一些现有代码进行修改时,或者修改现有B ...
- maven springTest结合junit单元测试
1.引入相关依赖 <dependency> <groupId>junit</groupId> <artifactId>junit</artifac ...
随机推荐
- jupyter notebook(二)——修改jupyter打开默认的工作目录
1.简述 jupyter notebook,启动后,浏览器发现工作目录并不是自己真正的代码的工作路径.所以需要设置一下.这样方便自己快捷使用. 2.设置修改jupyter notebook打开后默认工 ...
- Python的三种基本数据类型
数字 int(整型) long(长整型),python对长整型没有限制,理论上可以无限大.python3后没有long了. float 字符串 加了引号的都是字符串. 单引号和双引号没有约 ...
- CCPC_1005
可怕.....的提.....显而易见的规律活活没照出来...不过说起来却是不能严格证明....于是...脑筋急转弯活活猜不出来..... 1*1->1*2->2*2->2*3-> ...
- (PowerDesigner&Sqlite)PD中设计完表后,将其导入数据库中
本人连接过SQLServer跟SQLite Ⅰ.SQLServer,百度,转一下:http://jingyan.baidu.com/article/7f766daf465e9c4101e1d0d5.h ...
- shell脚本入门基础知识
shell 脚本的第一行 #!/bin/bash #!/bin/sh 其实,sh是bash的一个软链接 sh -> bash 变量,字母下划线开头(好像是没有类型的) #普通变量 var1=ni ...
- Codeforces 678F Lena and Queries
题意: 你有一个点集,有三种操作: 往集合里插入一个点\((x, y)\) 从集合中删除第\(i\)次操作插入的点 对于给出的\(q\),询问点集中\(x \cdot q + y\)的最大值 分析: ...
- Django 四——ModelForm用法
内容概要: 1.新增数据库表中数据 2.更新数据库表中数据 Django的ModelForm Django中内置了Form和Model两个类,有时候页面的表单form类与Model类是一一对应,因此分 ...
- ASP.NET Core 利用中间件支持跨域请求
方法1: 在Startup的ConfigureServices()中添加services.AddCors()在Startup的Configure()中添加app.UseCors(); 保证其在app. ...
- MySQL一对一:一对多:多对多
学生表和课程表可以多对多 一个学生可以学多门课程 一门课程可以有多个学生: 多对多 *** 一个学生对应一个班级 一个班级对应多个学生: 一对多 *** 一个老师对应多个学生 多个学生对应一个老师:一 ...
- Chrome autocomplete="off"无效
如果不希望输入框自动填充,可以设置 input 或 textarea 标签的属性 autocomplete="off". 但是有时 Chrome 会忽视 autocomplete ...