【转】SpringTest框架JUnit单元测试用例获取ApplicationContext实例的方法
转自: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实例的方法的更多相关文章
- SpringTest框架JUnit单元测试用例获取ApplicationContext实例的方法
步骤 1.继承AbstractJUnit4SpringContextTests 2.引入ApplicationContext 示例代码 1 2 3 4 5 6 7 8 9 10 11 12 13 14 ...
- spring-test跟junit结合单元测试获取ApplicationContext实例的方法
步骤 1.继承AbstractJUnit4SpringContextTests 2.引入ApplicationContext 示例代码:(可以根据name或者类型获取bean) import or ...
- spring获取ApplicationContext对象的方法——ApplicationContextAware
一. 引言 工作之余,在看一下当年学的spring时,感觉我们以前都是通过get~ set~方法去取spring的Ioc取bean,今天就想能不能换种模型呢?因为我们在整合s2sh时,也许有那么一天就 ...
- 获取applicationContext对象的方法
方法一:在初始化时保存ApplicationContext对象 代码: ApplicationContext ac = new FileSystemXmlApplicationContext(&quo ...
- spring 框架通过new Object()获取applicationContext 中的bean方案
工作中,需要手动创建一个对象,但用到了spring容器中对象的业务逻辑,这时候就要去通过获取容器中的对象.这时候,可以通过实例化一个实现了ApplicationContextAware接口的类获取sp ...
- 教你快速写出多线程Junit单元测试用例 - GroboUtils
摘自: http://mushiqianmeng.blog.51cto.com/3970029/897786/ 本文出自One Coder博客,转载请务必注明出处: http://www.coderl ...
- spring框架中由FactoryBean获取JedisCluster实例
spring配置文件: <bean id="jedisCluster" class="com.pingan.ela.structure.ex.JedisCluste ...
- Spring容器中获取bean实例的方法
// 得到上下文环境 WebApplicationContext webContext = ContextLoader .getCurrentWebApplicationContext(); // 使 ...
- 【spring框架】spring获取webapplicationcontext,applicationcontext几种方法详解--(转)
方法一:在初始化时保存ApplicationContext对象代码:ApplicationContext ac = new FileSystemXmlApplicationContext(" ...
随机推荐
- 警告 - no rule to process file 'WRP_CollectionView/README.md' of type net.daringfireball.markdown for architecture i386
warning: no rule to process file '/Users/mac/Downloads/Demo/Self/WRP_CollectionView/WRP_CollectionVi ...
- 对象映射组件Tiny Mapper
1.Tiny Mapper的简单实用例子 using System; using System.Collections.Generic; using System.Linq; using System ...
- Power BI Q&A终于在圣诞前夕盼到
相信跟所有的数据分析师们一样,赶上年底和年初都是非常忙的时候,即使赶上哪天运气好不加班每天回到家吃完饭恨不得倒在床上就美美的睡上一觉.本人也是如此,正直疲惫之际,尹相志在微博上把我一圈,说Power ...
- C#调用ArcGIS REST服务
ArcGIS REST API提供了简单.开放的接口来访问和使用ArcGIS Server发布的服务.使用ArcGIS REST API通过URL可以获取和操作每一个服务中的所有资源和操作. 1.使用 ...
- GENERATED_UCLASS_BODY 和 GENERATED_BODY 区别
the GENERATED_BODY() macro allows the class to build without having a constructor defined. If you ne ...
- Unity3D项目开发一点经验
我们主要使用3dsmax2010进行制作,输出FBX的类型导入Unity3D中.默认情况下,3dsmax8可以和U3D软件直接融合,自动转换为FBX物体. 注意事项如下: 1.面数控制 在MAX软件中 ...
- hibernate 关联映射
关联关系大致分为两大类: 1.单向关系:只需单向访问关联端.例如:只能通过老师访问学生,或者只能通过学生访问老师. 2.双向关系:关联的两端可以互相访问.例如:老师和学生之间可以互相访问. 单向关联可 ...
- APP设计师拿到APP产品原型开始,七步搞定APP设计(转)
任何一款成功的APP都需要以坚实的产品概念作为基础,因为概念决定了产品最终完成的潜力. 一般情况下,交到app设计师手里的都是移动app产品原型图.当然这个是在移动产品经理反复斟酌,并且与大家开会讨论 ...
- rsync+inotify实现实时同步案例--转
转自:http://chocolee.blog.51cto.com/8158455/1400596 随着应用系统规模的不断扩大,对数据的安全性和可靠性也提出的更好的要求,rsync在高端业务系统中也逐 ...
- 网上下载的CHM帮助文件打不开的解决办法。
我的机器 装的是 Windows server 2008 操作系统.他的安全性比较高. 我在网上下载了一个 CHM 帮助文档.结果打不开. 现象: 打开时 ,提示 安全警告, 提示:来自Interne ...