主要步骤

1. 在工程的pom文件中增加spring-test的依赖:

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.version}</version>
</dependency>

2. 使用springframework提供的单元测试

新建测试类,并在该类上加上两个注解:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"classpath*:ApplicationContext.xml"})

@RunWith 大家并不陌生,junit4里用它来做junit加载器

@ContextConfiguration 主要用来加载spring的配置文件路径:是一个字符串数组,可以加载多个spring配置文件

测试代码如下:

 import static org.junit.Assert.assertEquals;

 import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; @RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:ApplicationContext.xml"})
public class EmpolyeeTest {
@Autowired
ApplicationContext ctx; @Test
public void testEmployee(){
Employee employee =(Employee) ctx.getBean("employee");
assertEquals("zhangsan",employee.getName());
} }

3. 封装基于AbstractJUnit4SpringContextTests的测试基类

 import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.context.ApplicationContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; @RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"classpath*:ApplicationContext.xml"})
public class SpringTest extends AbstractJUnit4SpringContextTests { public <T> T getBean(Class<T> type){
return applicationContext.getBean(type);
} public Object getBean(String beanName){
return applicationContext.getBean(beanName);
}
protected ApplicationContext getContext(){
return applicationContext;
}

然后其他测试类只需要继承该类即可,可以省去每次都要绑定Application对象。

4. 当项目变得复杂,其中的spring配置文件被拆分成了多个,这样该如何引入多个配置文件呢?如下:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath*:spring-ctx-service.xml", "classpath*:spring-ctx-dao.xml" })

这样就可以轻松的引入多个spring的配置文件了。或者配置符合某一个正则表达式的一类文件,如:

 @RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath*:spring-ctx-*.xml")

Maven下使用Junit对Spring进行单元测试的更多相关文章

  1. 使用Junit对Spring进行单元测试实战小结

    Demo代码: @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = "classpath*:/ ...

  2. Maven下org.junit.Test无法使用

    原文地址: https://blog.csdn.net/allenChenZhiMing/article/details/81412983 我在看Spring in action(第四版)的时候,看到 ...

  3. Spring Boot 单元测试示例

    Spring 框架提供了一个专门的测试模块(spring-test),用于应用程序的单元测试. 在 Spring Boot 中,你可以通过spring-boot-starter-test启动器快速开启 ...

  4. 详解intellij idea搭建SSM框架(spring+maven+mybatis+mysql+junit)(下)

    在上一篇(详解intellij idea 搭建SSM框架(spring+maven+mybatis+mysql+junit)(上))博文中已经介绍了关于SSM框架的各种基础配置,(对于SSM配置不熟悉 ...

  5. 详解intellij idea搭建SSM框架(spring+maven+mybatis+mysql+junit)(上)

    SSM(Spring+SpringMVC+MyBatis)框架集由Spring.SpringMVC.MyBatis三个开源框架整合而成,常作为数据源较简单的web项目的框架. 其中spring是一个轻 ...

  6. Maven 跳过Junit单元测试

    转载自:https://blog.csdn.net/arkblue/article/details/50974957 -DskipTests,不执行测试用例,但编译测试用例类生成相应的class文件至 ...

  7. Spring MVC -- 单元测试和集成测试

    测试在软件开发中的重要性不言而喻.测试的主要目的是尽早发现错误,最好是在代码开发的同时.逻辑上认为,错误发现的越早,修复的成本越低.如果在编程中发现错误,可以立即更改代码:如果软件发布后,客户发现错误 ...

  8. spring+hibernate单元测试案例

    1,maven创建web工程 2,导入相关依赖 pom.xml <project xmlns="http://maven.apache.org/POM/4.0.0" xmln ...

  9. maven+springMVC+mybatis+junit详细搭建过程 ***

    springMVC+mybatis框架搭建 在上一遍博客中以及讲诉了新建maven项目的流程,现在紧跟上一遍文章,接着搭建spring项目 首先我们先要弄清搭建项目的一般流程,需要注意哪些方面,想要什 ...

随机推荐

  1. HDU 4489(DP)

    http://acm.hdu.edu.cn/showproblem.php?pid=4489 解题思路这里已经说的很清楚了: http://blog.csdn.net/bossup/article/d ...

  2. python学习之老男孩python全栈第九期_day022知识点总结——初识面向对象

    一. 面向对象的引入# 人狗大战 def person(name,HP,aggr,sex): person = { 'name':name, # 昵称 'HP':HP, # 生命值 'aggr':ag ...

  3. PoPo数据可视化周刊第3期 - 台风可视化

    9月台风席卷全球,本刊特别选取台风最佳可视化案例,数据可视化应用功力最深厚者,当属纽约时报,而传播效果最佳的是The Weather Channel关于Florence的视频预报,运用了数据可视化.可 ...

  4. COGS2217 papertask

    传送门 以前看到这题的时候觉得是道好题啊……然而今天没多久就做出来了= =(装B 表示并没有看懂其他人写的是什么做法,感觉我的做法好奇怪…… 我的做法是这样的: 首先给括号配对,不难发现所有括号串要么 ...

  5. ionic--配置路由

    1.ng-route index中引用文件: <script src="ionic.bundle.js"></script> <script src= ...

  6. css 各种常见布局整理

    在学习各种布局之前我们先来认识各个关键词,理解这些关键词,然后由点到面,这样就简单多了. display属性 页面中每个元素都有一个默认的display属性,它的值与该元素的类型有关,默认值通常是 b ...

  7. 《Linux命令行与Shell脚本编程大全第2版》读书笔记

    公司说不准用云笔记了,吓得我赶紧把笔记贴到博客上先..... 近3年前的了,只有一半的章节,后面的没空记录了.... 第1章 可以cat /proc/meminfo文件来观察Linux系统上虚拟内存的 ...

  8. vue2 使用 element-ui

    看了  http://element.eleme.io/#/zh-CN/component/installation     一些组件和样式够用了 , 试了下 element-ui ,配合到vue中 ...

  9. weex常用属性梳理

    之前发了一篇weex集成和开发的博客,主要是讲了weex开发环境的搭建和文件的编译.部署,还有就是一些个人对weex的理解,最近将原生的项目改造成weex的项目,也持续了有两个多月的时间了,后面我会发 ...

  10. 纯C语言跑分(详细注释)

    #include <stdio.h> #include <time.h>//clock()所属头文件 ;//快排的数据规模 ,N=;//整点.浮点运算的规模 ;//计算圆周率的 ...