ALOToverflow
ALOToverflow

Reputation: 2699

Retrieving context configuration at runtime


I'm facing a little issue regarding the @ContextConfiguration annotation with Spring 3.0.4. I would like to retrieve them 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

Upvotes: 0

Views: 764

Answers (3)

AngerClown
AngerClown

Reputation: 6229

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.

Upvotes: 0

FrVaBe
FrVaBe

Reputation: 49361

Check if implementing the ApplicationContextAware Interface will help you. This should offer you access to the application context.

Upvotes: 0

Bozho
Bozho

Reputation: 597382

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();

Upvotes: 3

Related Questions