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

JUnit单元测试用例中使用Spring框架,直接方式如下。

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "/spring/applicationContext.xml" })
public class SpringTest {}

想要获取ApplicationContext实例。可以添加了对ApplicationContext的注入

RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "/spring/applicationContext.xml" })
public class SpringTest { @Autowired
protected ApplicationContext ctx;

Spring中早已直接提供了更加方便使用的基类:AbstractJUnit4SpringContextTests

@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;
} }

本人补充:

若在静态类中需要引用只能使用如下方法:

public class TPlatformInfoTest extends TestBase{

    private static MyHttpClient TPlatformInfoDispose;

    @BeforeClass
public static void setUpBeforeClass(){
System.out.println("***********here init first***************");
TPlatformInfoDispose = new MyHttpClient("platform");
DataSourceContextHolder.setDbType("ds2");
ApplicationContext ctx = new ClassPathXmlApplicationContext("classpath:WEB-INF/rest-*.xml");
pfm = ctx.getBean(TPlatformInfoMapper.class);
dim = ctx.getBean(TDeviceInfoMapper.class);
}

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

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

    步骤 1.继承AbstractJUnit4SpringContextTests 2.引入ApplicationContext 示例代码 1 2 3 4 5 6 7 8 9 10 11 12 13 14 ...

  2. spring-test跟junit结合单元测试获取ApplicationContext实例的方法

    步骤 1.继承AbstractJUnit4SpringContextTests 2.引入ApplicationContext   示例代码:(可以根据name或者类型获取bean) import or ...

  3. spring获取ApplicationContext对象的方法——ApplicationContextAware

    一. 引言 工作之余,在看一下当年学的spring时,感觉我们以前都是通过get~ set~方法去取spring的Ioc取bean,今天就想能不能换种模型呢?因为我们在整合s2sh时,也许有那么一天就 ...

  4. 获取applicationContext对象的方法

    方法一:在初始化时保存ApplicationContext对象 代码: ApplicationContext ac = new FileSystemXmlApplicationContext(&quo ...

  5. spring 框架通过new Object()获取applicationContext 中的bean方案

    工作中,需要手动创建一个对象,但用到了spring容器中对象的业务逻辑,这时候就要去通过获取容器中的对象.这时候,可以通过实例化一个实现了ApplicationContextAware接口的类获取sp ...

  6. 教你快速写出多线程Junit单元测试用例 - GroboUtils

    摘自: http://mushiqianmeng.blog.51cto.com/3970029/897786/ 本文出自One Coder博客,转载请务必注明出处: http://www.coderl ...

  7. spring框架中由FactoryBean获取JedisCluster实例

    spring配置文件: <bean id="jedisCluster" class="com.pingan.ela.structure.ex.JedisCluste ...

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

    // 得到上下文环境 WebApplicationContext webContext = ContextLoader .getCurrentWebApplicationContext(); // 使 ...

  9. 【spring框架】spring获取webapplicationcontext,applicationcontext几种方法详解--(转)

    方法一:在初始化时保存ApplicationContext对象代码:ApplicationContext ac = new FileSystemXmlApplicationContext(" ...

随机推荐

  1. Foreign key (referential) constraints on DB2 LUW v105

    oreign key constraints (also known as referential constraints or referential integrity constraints) ...

  2. 《Thinking in Java》十七章_容器深入研究_练习14(Page486)

    练习14 Properties的继承树如下:

  3. MySQL中varchar转int

    order by ... cast(sort as signed) 或 convert(sort,signed) (sort为待转化字段)

  4. C#集合类型

    using System; using System.Collections; using System.Collections.Generic; namespace codeTest { class ...

  5. 二叉树学习笔记之经典平衡二叉树(AVL树)

    二叉查找树(BSTree)中进行查找.插入和删除操作的时间复杂度都是O(h),其中h为树的高度.BST的高度直接影响到操作实现的性能,最坏情况下,二叉查找树会退化成一个单链表,比如插入的节点序列本身就 ...

  6. MSMQ创建消息队列出现“工作组安装计算机不支持该操作”

    [sceislqzw]:你在创建公有队列,而你的机器不属于任何域.一般工作组安装的计算机只能创建私有队列. System.Messaging.MessageQueue QueueReceive = n ...

  7. AR , VR, GVR ...

    虚拟现实与增强现实(眼镜或头盔)的现状与未来-简介http://blog.csdn.net/yanzhanyi/article/details/41982033 Google VR  |  Googl ...

  8. poj 2262【素数表的应用---判断素数】【哈希】

    Goldbach's Conjecture Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 35214   Accepted: ...

  9. Spring XML配置文件示例(二)——web.xml

    <?xml version="1.0" encoding="UTF-8"?> <web-app id="WebApp_ID" ...

  10. outlook备份及恢复

    outlook备份及恢复 Reference: http://wenku.baidu.com/link?url=2gtDkCSDoPdnfx3Ungd6on9wdhUTWgbO_vmmKLv1i4df ...