我有一个 Spring JUnit 测试器类MySimpleTester
:
@
RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"classpath:/spring/mySimpleConfig.xml"})
public class MySimpleTester {
@Before
public void setUp() throws Exception {
myAdapter = (MyAdapter) applicationContext.getBean("myAdapter");
}
@test
public void testGetSimpleList() {
List<SimpleLink> simpleList = **myAdapter.getSimpleLinksList**();
}
……
在适配器类中,我有:
public MyAdapter {
public List<SimpleLink> getSimpleLinksList() {
List<SimpleLink> simLinks = null;
String environment = AppFactory.getPropertiesObj();
……
class AppFactory implements ApplicationContextAware {
private static ApplicationContext context;
public void setApplicationContext(ApplicationContext acontext) {
context = acontext;
}
public getPropertiesObj() {
return getAppContext().getBean("propertiesBean");
}
我明白NullPointerException
了,ApplicationContext
就Null
在这里。
但是在SpringJUnitTestRunner
课堂上MySimpleTester
我可以找到正确初始化的 applicationContext 。我不包括mySimpleConfig.xml
和包含的文件。MyAdapter
当在应用程序服务器中运行时,类中的方法在getSimpleLinksList()
Web 应用程序中工作得非常好,并且在那里获得了 appcontext。
只有通过 Spring 测试器才能访问静态应用程序上下文AppFactory
类,因为它是通过AppFactory.getPropertiesObj()
. 当其他测试类正在执行时,我正确设置了类路径。