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 ...
随机推荐
- Mbps、Kbps、bps、kb、mb区别和换算
Mbps 即 Milionbit pro second(百万位每秒) Kbps 即 Kilobit pro second(千位每秒) bps 即 bit pro second(位每秒) 速度单位,bi ...
- html下拉菜单栏代码
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...
- Yii 2.x html 代码压缩
<?php namespace Pangu\web; use yii\base\Component; /** * html格式响应内容格式化 * @author zhouzhian * */ c ...
- 02 python网络爬虫《Http和Https协议》
一.HTTP协议 1.概念: Http协议就是服务器(Server)和客户端(Client)之间进行数据交互(相互传输数据)的一种形式. 之间形成的特殊行话(黑话:(土匪)天王盖地虎,(我)宝塔镇河妖 ...
- Storm: 遇到问题总结
1.没有ack : kafkaspout id 重复导致每次读最新没有数据. 2.由于storm提供的读取kafka的enternal工具存在bug,导致重复读取数据,致使数据不准确.storm bu ...
- Spring加载配置文件的几种方法(org.springframework.beans.factory.BeanDefinitionStoreException)
一:Spring中的几种容器都支持使用xml装配bean,包括:XmlBeanFactory ,ClassPathXmlApplicationContext ,FileSystemXmlApplica ...
- v-model 的修饰符
1..trim 自动过滤输入内容最开始 和 最后的 空格,中间的会保留一个空格,多的会被过滤掉 2..lazy 一般情况下,在input的 v-model是一直在同步 输入的内容与显示的内容,不过再添 ...
- java基础_单例模式
java开发实战经典 --单例模式 从CSDN以及博客园的相关文章学习的,摘做笔记. “java中单例模式是一种常见的设计模式,单例模式的写法有好几种,这里主要介绍三种:懒汉式单例.饿汉式单例.登记式 ...
- 测试环境docker化—容器集群编排实践
本文来自网易云社区 作者:孙婷婷 背景 在前文<测试环境docker化-基于ndp部署模式的docker基础镜像制作>中已经详述了docker镜像制作及模块部署的过程,按照上述做法已可以搭 ...
- 【Remove Duplicates from Sorted Array】cpp
题目: https://leetcode.com/problems/remove-duplicates-from-sorted-array/ Given a sorted array, remove ...