junit单位测试的几种方法
来源:互联网 发布:熊猫采集软件下载 编辑:程序博客网 时间:2024/04/29 22:29
Junit单元测试中获得spring bean
- 博客分类:
- Spring
- Java
主要介绍单元测试中获得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");
- }
- }
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>
<?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);
- }
- }
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);
- }
- }
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>
<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
- junit单位测试的几种方法
- junit测试延伸--方法的重复测试
- 编写JUnit测试的理由和几条测试习惯
- 编写JUnit测试的理由和几条测试习惯
- 编写JUnit测试的理由和几条测试习惯
- 编写JUnit测试的理由和几条测试习惯
- 编写JUnit测试的理由和几条测试经验
- junit测试web工程的简单方法
- Junit指定运行的测试方法
- Junit测试类常用的几个方法
- JUnit无法运行测试方法的记录
- Junit 笔记 一.对方法的测试
- 项目中添加Junit测试的方法
- 几种常见的长度单位整理
- 前端几种单位的比较
- 需求测试的几种方法
- 几种排序方法的测试!
- 几种测试工作量的估算方法
- python语句和控制流
- 小飞的电梯调度算法
- zen-Coding在Notepad++中的安装及使用
- Lucene索引操作常用类
- timer 的使用 模拟显示时间,可以每隔一段时间定时执行调度
- junit单位测试的几种方法
- Ubuntu 12.04 中自定义DNS服务器设置
- IOS 解析GBK编码
- Direct3D轮回:游戏场景之河流
- 解决server地址含有中文的方法
- 第十章 Libgdx音频之音乐流
- 802.3标准共包含有多少种协议?
- Android开发,常用的终端命令
- python表达式