user2740224
user2740224

Reputation:

ContextConfiguration RunWith WebAppConfiguration annotations together

I have class which annotated these annotations:

    @ContextConfiguration(locations = { "classpath:pathToXml.xml" })
    @RunWith(SpringJUnit4ClassRunner.class)
    @WebAppConfiguration

Can you explain what features I had after I added these annotation on my class ?

Upvotes: 8

Views: 4557

Answers (2)

Gwaptiva
Gwaptiva

Reputation: 395

So, battling with the need to read database creation scripts for the in-memory database from src/test/resources in tests annotated with @WebAppConfiguration, I noticed that in the mock application context created, the resourceLoader field was null. So, I created a custom ApplicationContextInitializer that sets this field to an AnnotationConfigApplicationContext instance.

Then all tests that need access to the regular resources just add something like @ContextConfiguration(initializers = { MyConfigurableContext.class })

Seems to do the trick, but obviously YMMV

Upvotes: 0

Boris Treukhov
Boris Treukhov

Reputation: 17784

You'll get the features described in @WebAppConfiguration configuration javadoc.

WebApplicationContext mostly changes the way resources are loaded, i.e. resources with unspecifed resource prefix will be loaded from src/main/webapp or from the location in value parameter(they won't be available in Spring context otherwise, because usually webapp folder is not included to classpath) instead of classpath:.

Also you will be able to to test the code which uses other WebApplicationContext features- ServletContextAware beans, Session and Request bean scopes etc.

That means that you will be able to use Spring MVC Test Framework

Upvotes: 3

Related Questions