开发者

Retrieving context configuration at runtime

开发者 https://www.devze.com 2023-01-29 16:55 出处:网络
I\'m facing a little issue regarding the @ContextConfiguration annotation with Spring 3.0.4. I would like to retrieve them 开发者_Python百科at runtime via the applicationContext (if possible).

I'm facing a little issue regarding the @ContextConfiguration annotation with Spring 3.0.4. I would like to retrieve them 开发者_Python百科at runtime via the applicationContext (if possible).

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消