码迷,mamicode.com
首页 > 移动开发 > 详细

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

时间:2016-09-22 14:28:32      阅读:219      评论:0      收藏:0      [点我收藏+]

标签:

步骤
1、继承AbstractJUnit4SpringContextTests
2、引入ApplicationContext


示例代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
 
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "/spring/applicationContext.xml" })
public class SpringTest extends AbstractJUnit4SpringContextTests {
 
    @Autowired
    protected ApplicationContext ctx;
 
    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;
    }
}





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

标签:

原文地址:http://www.cnblogs.com/gossip/p/5896058.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!