我有一个 Spring 测试使用:
@RunWith(SpringJUnit4ClassRunner.class)
与旧的测试方法不同,从 Spring 测试基类扩展,似乎没有明显的方法可以访问 Spring 使用 @ContextConfiguration
如何从我的测试方法中访问 ApplicationContext
对象?
谢谢!
最佳答案
来自 Integration Testing Spring 文档的部分
@Autowired ApplicationContext
As an alternative to implementing the ApplicationContextAware interface, you can inject the application context for your test class through the @Autowired annotation on either a field or setter method. For example:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration
public class MyTest {
@Autowired
private ApplicationContext applicationContext;
// class body...
}
关于java - 使用@RunWith(SpringJUnit4ClassRunner.class),可以访问ApplicationContext对象吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14822730/