`

Junit单元测试中获得spring bean

 
阅读更多

主要介绍单元测试中获得bean的三种方法,以及各自的优劣。其实跟开发时获得bean方法一样,如下:

a.通过ClassPathXmlApplicationContext得到ApplicationContext,再getBean

b.通过set函数获得bean

c.启用直接对保护类型属性变量进行注入的机制

日常应用中推荐大家使用第二、三中方法。尤其对于bean较多时,使用第三种可以方便省事很多。

 

所用依赖为junit 3.8.1和spring-test包2.5.6。下面为详细介绍:

 

1、通过ClassPathXmlApplicationContext得到ApplicationContext,再getBean

单元测试中需要引入bean,最容易想到的办法就是使用spring bean注入的原理,代码如下:

package com..*.*.*.test;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.test.AbstractDependencyInjectionSpringContextTests;

import com.*.*.*.TestService;

public class TestBean extends AbstractDependencyInjectionSpringContextTests {

    @Test
    public void testBeanInjection() {
         ApplicationContext testContext = new ClassPathXmlApplicationContext("test_services.xml");
         TestService testService = (TestService)testContext.getBean("testService");
    }
}

先通过ClassPathXmlApplicationContext bean xml得到ApplicationContext,再getBean。

但按照Spring的推荐,在单元测试时不应该依赖于Spring容器,也就是说不应该在单元测试时启动ApplicationContext并从中获取Bean,所以此种方法不推荐。建议大家继续看看下面两种

其中test_services.xml内容如下,下面两种方法一样。

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans default-autowire="byName">
	<bean id="testService"
		class="com.*.*.*.TestServiceImpl">
	</bean>
</beans>

 

2、通过set函数获得bean

通过getConfigLocations设置bean xml路径

新增setTestService设置函数,对testService赋值

注意testService的类型不限制

package com..*.*.*.test;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.test.AbstractDependencyInjectionSpringContextTests;

import com.*.*.*.TestService;

public class TestSendEmail extends AbstractDependencyInjectionSpringContextTests {

    private TestService testService;

    public void setTestService(TestService testService) {
        this.testService = testService;
    }

    /**
     * 设置bean xml路径,可以添加多个
     */
    protected String[] getConfigLocations() {
        return new String[] {"test_services.xml"};
    }

    @Test
    public void testBeanInjection() {
        assertTrue(testService != null);
    }
}

这种方式的好处在不用手动加载bean xml文件,但每个bean需要一个set函数,当bean较多时需要生成较多set函数

 

 

3、启用直接对保护类型属性变量进行注入的机制

通过getConfigLocations设置bean xml路径

新增构造函数,在其中调用setPopulateProtectedVariables(true)设置启用直接对保护类型属性变量进行注入的机制

注意testService为保护型

package com..*.*.*.test;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.test.AbstractDependencyInjectionSpringContextTests;

import com.*.*.*.TestService;

public class TestBean extends AbstractDependencyInjectionSpringContextTests {

    protected TestService testService;

    public TestBean(){
        // 启用直接对保护类型属性变量进行注入的机制
        this.setPopulateProtectedVariables(true);
    }

	/**
     * 设置bean xml路径,可以添加多个
     */
    protected String[] getConfigLocations() {
        return new String[] {"test_services.xml"};
    }

    @Test
    public void testBeanInjection() {
        assertTrue(testService != null);
    }
}

这种方式的好处在不用手动加载bean xml文件,也不用再写set函数 ,但需要一个构造函数并在其中启用直接对保护类型属性变量进行注入的机制

 

其他:

程序中依赖了spring-test的包,需要在pom中添加依赖如下

<dependency>
	<groupId>org.springframework</groupId>
	<artifactId>spring-test</artifactId>
	<version>2.5.6</version>
</dependency>

 参考:http://mvnrepository.com/artifact/org.springframework/spring-test/2.5

 

 

分享到:
评论

相关推荐

    idea +junit单元测试获取不到bean注入的解决方式

    主要介绍了idea +junit单元测试获取不到bean注入的解决方式,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧

    spring接管JUnit

    接管JUnit资源是指Spring框架为了方便进行单元测试,在测试类中提供了自动装配和依赖注入的功能,使得测试类能够轻松地使用Spring容器中的Bean和依赖。这样,我们可以在测试类中使用Spring的依赖注入机制来注入所需...

    spring-spock-integration-testing:如何将Spock模拟注入到Spring集成测试中

    在Spring Boot 1.4中,他们引入了很多很酷的测试玩具,包括@MockBean 。 遗憾的是,仅在使用JUnit进行测试时才支持。 值得庆幸的是,Spock 1.1引入了DetachedMockFactory 。 结合新的Spring Boot @Test...

    模拟实现Spring的IOC

    2、本工程,模拟实现Spring的IOC,将xml中配置的Bean读入到Spring容器中,并在测试用例中调用Spring容器中的bean。 3、开发环境eclipse,jdk1.6。使用第三方jar包有:jdom.jar(用于解析xml文件),junit-4.4.jar...

    Spring MVC 教程 快速入门 深入分析

    十三、如何给spring3 MVC中的Action做JUnit单元测试? 十四、spring mvc 转发与重定向 十五、spring mvc 处理ajax请求 十六、spring mvc 关于写几个配置文件的说明 十七、spring mvc 如何取得Spring管理的bean 十八...

    Spring MVC入门教程

    十三、如何给spring3 MVC中的Action做JUnit单元测试? 十四、spring mvc 转发与重定向 十五、spring mvc 处理ajax请求 十六、spring mvc 关于写几个配置文件的说明 十七、spring mvc 如何取得Spring管理的bean 十八...

    Spring in Action(第二版 中文高清版).part2

    B.2 单元测试Spring MVC控制器 B.2.1 模拟对象 B.2.2 断言ModelAndView的内容 B.3 使用Spring进行综合测试 B.3.1 测试装配后的对象 B.3.2 综合测试事务处理对象 B.3.3 测试数据库 B.3.4 使用Gienah Testing在...

    Spring in Action(第二版 中文高清版).part1

    B.2 单元测试Spring MVC控制器 B.2.1 模拟对象 B.2.2 断言ModelAndView的内容 B.3 使用Spring进行综合测试 B.3.1 测试装配后的对象 B.3.2 综合测试事务处理对象 B.3.3 测试数据库 B.3.4 使用Gienah Testing在...

    SpringMVC框架架构介绍

    十三、如何给spring3 MVC中的Action做JUnit单元测试? 十四、spring mvc 转发与重定向 十五、spring mvc 处理ajax请求 十六、spring mvc 关于写几个配置文件的说明 十七、spring mvc 如何取得Spring管理的bean ...

    spring-test-4.1.2.RELEASE-sources.jar

    因为所有的Bean都需要在applicationContext.xml中加载好,之后再通过@Resource去取得。如果每次都要整个业务流做的差不多了再去测试,这样效率很低,也很麻烦。如果单独去写一个测试用类,这样太繁琐了。于是想起...

    从零开始学Spring Boot

    1.30 Spring Boot Junit单元测试 1.31 SpringBoot启动时的Banner设置 1.32 Spring boot 文件上传(多文件上传) 1.33 导入时如何定制spring-boot依赖项的版本 1.34 Spring Boot导入XML配置 1.35 Spring Boot使用@...

    SpringMVC入门教程

    十三、 如何给spring3 MVC中的Action做JUnit单元测试 十四、 spring mvc 转发与重定向 (带参数重定向) 十五、 spring mvc 处理ajax请求 十六、 spring mvc 关于写几个配置文件的说明 十七、 spring mvc 如何取得...

    Spring.3.x企业应用开发实战(完整版).part2

    16.7.1 对LoginController进行单元测试 16.7.2 使用Spring Servlet API模拟对象 16.7.3 使用Spring RestTemplate测试 16.7.4 使用Selenium测试 16.8 小结 第17章 实战案例开发 17.1 论坛案例概述 17.1.1 论坛整体...

    Spring+3.x企业应用开发实战光盘源码(全)

     第16章:有别于一般书籍的单元测试内容,本书以当前最具实战的JUnit4+Unitils+ Mockito复合测试框架对如何测试数据库、Web的应用进行了深入的讲解。  第17章:以一个实际的项目为蓝本,带领读者从项目需求分析、...

    Spring in Action(第2版)中文版

    b.2单元测试springmvc控制器 b.2.1模拟对象 b.2.2断言modelandview的内容 b.3使用spring进行综合测试 b.3.1测试装配后的对象 b.3.2综合测试事务处理对象 b.3.3测试数据库 b.3.4使用gienahtesting在junit4中...

    making-sense-dependency-injection-test-execution-listener:Spring JUnit Spock的测试侦听器,以访问@BeforeClass中的bean

    该侦听器对于Spring / JUnit测试以及基于JUnit的测试框架实现(例如 )最有用。 它将确保在调用@BeforeClass之前预加载Spring ApplicationContext并自动装配所有bean(按类型)。 #为什么我会需要它? 在以下...

    Spring中文帮助文档

    8.2.2. 单元测试支持类 8.3. 集成测试 8.3.1. 概览 8.3.2. 使用哪个支持框架 8.3.3. 通用目标 8.3.4. JDBC测试支持 8.3.5. 常用注解 8.3.6. JUnit 3.8遗留支持 8.3.7. Spring TestContext Framework 8.3.8....

    Spring+Junit4进行接口测试实例代码

    主要介绍了Spring+Junit4进行接口测试实例代码,涉及pom.xml、bean的配置,以及接口测试代码等相关内容,小编觉得还是挺不错的,这里分享给大家,需要的朋友可以参考下

    Spring攻略(第二版 中文高清版).part1

    3.7 在你的Bean中引入行为 132 3.7.1 问题 132 3.7.2 解决方案 132 3.7.3 工作原理 132 3.8 为你的Bean引入状态 135 3.8.1 问题 135 3.8.2 解决方案 135 3.8.3 工作原理 135 3.9 用基于XML的配置...

    Spring5.0官方中文文档

    Spring5.0官方中文文档新特性: 升级到 Java SE 8 和 Java EE 7 反应式编程模型 ...使用 JUnit 5 执行条件和并发测试 使用 Spring WebFlux 执行集成测试 包清理和弃用 对 Spring 核心和容器的一般更新

Global site tag (gtag.js) - Google Analytics