SpringTest框架JUnit单元测试用例获取ApplicationContext实例的方法

JUnit单元测试用例中使用Spring框架,之前我的使用方式很直接。

/**
 * 用于需要用到Spring的测试用例基类
 *
 * @author lihzh
 * @alia OneCoder
 * @blog http://www.coderli.com
 */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "/spring/applicationContext.xml" })
public class SpringTest {}

在测试的过程中,有人提到,想要获取ApplicationContext实例。于是,添加了对ApplicationContext的注入。

**
 * 用于需要用到Spring的测试用例基类
 *
 * @author lihzh
 * @alia OneCoder
 * @blog http://www.coderli.com
 */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "/spring/applicationContext.xml" })
public class SpringTest {

@Autowired
protected ApplicationContext ctx;

其实,Spring中早已直接提供了更加方便使用的基类:AbstractJUnit4SpringContextTests。修改代码如下:

/**
 * 用于需要用到Spring的测试用例基类
 *
 * @author lihzh
 * @alia OneCoder
 * @blog http://www.coderli.com
 */
@ContextConfiguration(locations = { "/spring/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;
}

}

代码也简洁多了。

现在想想,你想要的常用功能,一般人家都能想到了。做之前,不妨先查查有没有现成可用的工具吧:)

转自:http://www.coderli.com/junit-spring-test-applicationcontext/

时间: 2024-08-25 02:34:05

SpringTest框架JUnit单元测试用例获取ApplicationContext实例的方法的相关文章

【转】SpringTest框架JUnit单元测试用例获取ApplicationContext实例的方法

转自:http://www.coderli.com/junit-spring-test-applicationcontext JUnit单元测试用例中使用Spring框架,直接方式如下. @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = { "/spring/applicationContext.xml" }) public class SpringTest {} 想要获取Applicat

获取applicationContext对象的方法

方法一:在初始化时保存ApplicationContext对象 代码: ApplicationContext ac = new FileSystemXmlApplicationContext("applicationContext.xml"); ac.getBean("beanId"); 说明:这种方式适用于采用Spring框架的独立应用程序,需要程序通过配置文件手工初始化Spring的情况. 方法二:通过Spring提供的工具类获取ApplicationConte

Spring容器中获取bean实例的方法

// 得到上下文环境 WebApplicationContext webContext = ContextLoader .getCurrentWebApplicationContext(); // 使用上下文环境中的getBean方法得到bean实例 InhospDoctorStationController controller = (InhospDoctorStationController) webContext.getBean("inhospDoctorStationController

Spring 使用实例工厂方法实例化Bean

知识点介绍: 实例工厂的意思是获取对象实例的方法不是静态的,所以你需要首先new工厂类,再调用普通的实例方法. [转载使用,请注明出处:http://blog.csdn.net/mahoking] 操作步骤: 1.创建Speaker对象. public class Speaker { //使用实例工厂方法实例化Bean private String speakerName; public Speaker(String speakerName) { this.speakerName = speak

Spring测试框架JUnit搭建测试环境 不通过web服务器 初始化spring bean对象

直接使用 JUnit 测试 Spring 程序存在的不足 需要使用硬编码方式手工获取 Bean:在测试用例中,我们需要通过 ApplicationContext.getBean() 的方法从 Spirng 容器中获取需要测试的目标 Bean,并且还要进行造型操作. 数据库现场容易遭受破坏:测 试方法可能会对数据库记录进行更改操作,破坏数据库现场.虽然是针对开发数据库进行测试工作的,但如果数据操作的影响是持久的,将会形成积累效应并影响到 测试用例的再次执行.举个例子,假设在某个测试方法中往数据库插

Spring获取ApplicationContext方式,和读取配置文件获取bean的几种方式

转自:http://chinazhaokeke.blog.163.com/blog/static/109409055201092811354236  Spring获取ApplicationContext方式 我自己常用的方法: 读取一个文件1 //创建Spring容器 2 ApplicationContext ctx = new ClassPathXmlApplicationContext("bean.xml"); 3 //获取chinese 实例 4 Person p = ctx.g

spring中获取applicationContext

常用的5种获取spring 中bean的方式总结: 方法一:在初始化时保存ApplicationContext对象代码:ApplicationContext ac = new FileSystemXmlApplicationContext("applicationContext.xml");ac.getBean("beanId");说明:这种方式适用于采用Spring框架的独立应用程序,需要程序通过配置文件手工初始化Spring的情况. 方法二:通过Spring提供

spring获取webapplicationcontext,applicationcontext几种方法详解

http://www.blogjava.net/Todd/archive/2010/04/22/295112.html 方法一:在初始化时保存ApplicationContext对象代码:ApplicationContext ac = new FileSystemXmlApplicationContext("applicationContext.xml");ac.getBean("beanId");说明:这种方式适用于采用Spring框架的独立应用程序,需要程序通过

spring获取webapplicationcontext,applicationcontext几种方法详解[转载]

方法一:在初始化时保存ApplicationContext对象 代码: ApplicationContext ac = new FileSystemXmlApplicationContext("applicationContext.xml"); ac.getBean("beanId"); 或者通过classpath 路径获取 ApplicationContext ac = new ClassPathXmlApplicationContext(new String[]