Retrieving context configuration at runtime
Let's say I have this class :
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"foo.xml", "bar.xml"})
public class Foo() extends AbstractTransactionalJUnit4SpringContextTests {
@Before
public void before() {
//retrieve foo.xml and bar.xml here
}
}
It would be usefull in my situation, I've looked on the web a little and haven't found a lot of ideas on how to achieve this, maybe SO experts can help me on that.
Thanks
I'm not aware of having a public utility for that, but the easiest way would be to manually parse the annotation:
String[] locations =
Foo.class.getAnnotation(ContextConfiguration.class).locations();
Check if implementing the ApplicationContextAware Interface will help you. This should offer you access to the application context.
You can't dynamically specify the locations in the @ContextConfig annotation. But, you can use a TestExecutionListener instead, along with the appropriate annotation. In beforeTestMethod(), the listener could get the ApplicationContext from the TestContext and load the necessary xml files manually.
If you want different xml files for each test, you should also call TestContext.markApplicationContextDirty() in an @After method too.
精彩评论