I have a Spring JUnit tester class 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**();
}
... ...
In the adapter class I have:
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");
}
I get NullPointerException
and see that ApplicationContext
is Null
here.
However at the SpringJUnitTestRunner
class MySimpleTester
I could find the applicationContext to be initialized correctly. I am not including the mySimpleConfig.xml
and included files. The method in MyAdapter
class getSimpleLinksList()
works perfectly fine from the web application when run in the application server, and the appcontext is obtained there.
Only from the Spring tester is it not able to reach the static application context AppFactory
class, as it is called statically through AppFactory.getPropertiesObj()
. I had the classpath set correctly as other test classes are executing.
If you want to access the current ApplicationContext in MySimpleTester:-