Spring框架是无侵入性的,所以你的代码可以完全是POJO(plain old java object),直接使用Junit就可以完成大部分的单元测试。但是在集成测试方面就比较吃力了。单元测试层面你可以mock一些依赖对象,但是集成测试时需要真实的依赖对象,而这些对象都是在Spring容器的控制之下。那么如何在引入了Spring的情况下进行集成测试那?别着急,Spring框架早为我们想到了这点,本身提供了集成测试的功能。

就拿上一次那个简单的例子来做实验吧。

首先引入对junit以及spring-test库的依赖。

pom.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
   <dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>4.0.2.RELEASE</version>
</dependency>
</dependencies>

spring-test模块是专门为使用了spring框架的项目进行集成测试的辅助类库。其有以下几个目的。

  • 提供在运行测试时对Spring IOC容器的缓存,提高集成测试速度。

  • 对测试实例提供依赖注入功能。

  • 集成测试中提供事务管理。

  • 提供一些辅助类库帮助开发者更好的编写集成测试。

然后新建一个ApplicationTest.java类,代码如下所示。

ApplicationTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
package huangbowen.net;

import huangbowen.net.service.Cinema;
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; import static org.junit.Assert.assertNotNull; @RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = {Application.class})
public class ApplicationTest { @Autowired
private ApplicationContext applicationContext; @Autowired
private Cinema cinema; @Test
public void shouldGetCinemaInstance() {
Cinema cinema = applicationContext.getBean(Cinema.class);
assertNotNull(cinema);
} @Test
public void shouldGetAutowiredCinema() {
assertNotNull(cinema);
} @Test
public void shouldGetMovieServiceInstance() {
assertNotNull(cinema.getMovieService());
assertThat(cinema.getMovieService(), instanceOf(DefaultMovieService.class));
}
}

在本例中ApplicationTest有两个注解。@RunWith(SpringJUnit4ClassRunner.class).是Spring TestContext 框架提供的一个自定义的JUnit runner,这样在测试类中就可以获取ApplicationContext,甚至直接进行依赖注入,使用事务控制测试方法执行等。声明了@RunWith(SpringJUnit4ClassRunner.class)以后一般还要声明@ContextConfiguration注解。这个注解是用于告诉测试类本项目中的Spring配置。这里我们传入Application.class类,因为这个类中配置了Spring的bean。

然后就可以在测试类中使用强大的@Autowired功能了。我们写了三个测试方法,第一个是通过Autowired功能拿到ApplicationContext,第二个是通过Autowired功能直接拿到cinema,第三个则是验证Cinema中的MovieService是被正确注入了的。

Ok,今天就到这里。本例中的源码请在我的GitHub上自行下载。

Spring-Context之二:使用Spring提供的测试框架进行测试的更多相关文章

  1. Spring Boot(十二):spring boot如何测试打包部署

    Spring Boot(十二):spring boot如何测试打包部署 一.开发阶段 1,单元测试 在开发阶段的时候最重要的是单元测试了,springboot对单元测试的支持已经很完善了. (1)在p ...

  2. Spring Boot 2 (二):Spring Boot 2 动态 Banner

    Spring Boot 2 (二):Spring Boot 2 动态 Banner Spring Boot 2.0 提供了很多新特性,其中就有一个小彩蛋:动态 Banner. 一.配置依赖 使用 Sp ...

  3. Spring学习(二)——Spring中的AOP的初步理解[转]

      [前面的话] Spring对我太重要了,做个关于web相关的项目都要使用Spring,每次去看Spring相关的知识,总是感觉一知半解,没有很好的系统去学习一下,现在抽点时间学习一下Spring. ...

  4. Spring学习(二)——Spring中的AOP的初步理解

    [前面的话] Spring对我太重要了,做个关于web相关的项目都要使用Spring,每次去看Spring相关的知识,总是感觉一知半解,没有很好的系统去学习一下,现在抽点时间学习一下Spring.不知 ...

  5. Spring知识点总结(二)之Spring IOC

    1.创建bean类,并在spring中进行配置交由spring来管理1. IOC(DI) - 控制反转(依赖注入)    所谓的IOC称之为控制反转,简单来说就是将对象的创建的权利及对象的生命周期的管 ...

  6. Spring实战(二)Spring容器和bean的生命周期

    引入问题: 在XML配置文件中配置bean后,这些文件又是如何被加载的?它们被加载到哪里去了? Spring容器——框架核心 1.什么是Spring容器?它的功能是什么? 在基于Spring的应用中, ...

  7. Spring学习(二)--Spring的IOC

    1.依赖反转模式 依赖反转:高层次的模块不应该依赖于低层次的模块,两者都应该依赖于抽象接口.抽象接口不应该依赖于具体实现.而具体实现则应该依赖于抽象接口. 在面向对象编程领域中,依赖反转原则(Depe ...

  8. Spring学习(二)Spring的bean管理(XML)

    Bean的实例化方式 1.在Spring里面通过配置文件创建对象 2.bean实例化的三种方式第一种:使用类的无参数构造函数创建(最常用的方式,第2种和第3种方法一般不用) 如果类里面没有无参的构造函 ...

  9. Spring Cloud(十二):Spring Cloud Zuul 限流详解(附源码)(转)

    前面已经介绍了很多zuul的功能,本篇继续介绍它的另一大功能.在高并发的应用中,限流往往是一个绕不开的话题.本文详细探讨在Spring Cloud中如何实现限流. 在 Zuul 上实现限流是个不错的选 ...

随机推荐

  1. flex4.0密钥及破解方式

    输入下面的序列号: 1424-4507-0757-7016-8907-6937 1424-4785-4428-8084-6314-8733 1424-4794-9281-8063-2338-9079 ...

  2. anomaly detection algorithm

    anomaly detection algorithm 以上就是异常监测算法流程

  3. gdb调式

    1.PCB版的相应目录下执行命令: gdbserver 10.18.13.84:5555 DvdPlayer 2.linux操作系统执行:(如果是android找到android项目路径下的gdb)m ...

  4. DUILIB CDialogBuilder 使用问题

    频繁调用CDialogBuilder的create接口创建同一个配置文件,会报异常: 正常的处理方式如下: if (!m_dlgBuilder.GetMarkup()->IsValid()) { ...

  5. XML的Pull解析

    //通过xml解析串    private void XMLtoStr(String result) {        News news=null;        try {             ...

  6. Markdown常用基本语法

    现在是我在学习Markdown时做的笔记.学完这些Markdown的基本使用已经不成问题. 1. 标题设置(让字体变大,和word的标题意思一样)在Markdown当中设置标题,有两种方式:第一种:通 ...

  7. iOS.AppThinning-iOS9-new-feature-for-app-thinning-bitcode-odr-slicing

    Bitcode 0. Introduction to Bitcode 1. Build static library or framework via Xcode 7, while user buil ...

  8. quick-cocos2d-x之testlua之VisibleRect.lua

    require "extern" --这个类找到了可视区域的9个点的坐标:左上.上的中点.右上.左的中点.左下.下的中点.右下.右的中点.一般用于使用相对坐标的场合,解决自适应屏幕 ...

  9. Multi-line NSAttributedString with truncated text

    http://stackoverflow.com/questions/7611816/multi-line-nsattributedstring-with-truncated-text/1017279 ...

  10. ADB工具常用指令和使用情形分析

    一.ADB常用命令:(不需要进到设备,也就是:不需要执行adb shell) adb devices 列出所有的已连接的设备 adb kill-server 关闭adb adb start-serve ...