I have created 2 classes A,B for JUnit, each has its own ContextLoaders for loading the application context. Now when I run each of these classes independently the JUnit test run fine.
But when I do a maven build or run the JUnit on both of these classes the one which runs first say A, runs perfectly fine but the second one throws an exception (could not autowire field....expected one found none...). here is what i'm trying to do:-
//Class A with Context loader A_ContextLoader
@runwith(springjunit4classrunner.class)
@ContextConfiguration(loader = A_ContextLoader.class)
public class A {
@Test
public void testMethod1() {
...
}
}
//Class B with Context loader B_ContextLoader
@runwith(springjunit4classrunner.class)
@ContextConfiguration(loader = B_ContextLoader.class)
public class B {
@Test
public void testMethod2() {
...
}
}
From what I understand, when the execution starts for the 2nd class it uses the existing context instead of initializing a new one. In my case it is not possible that i club both the context loaders into a single class.
So is there some way I can "unload" the application context at the end of execution of class A. Or some how ask JUnit to reinitialize the context when execution for B starts? Or Am I missing out something?