PDA

View Full Version : @ContextConfiguration does not take a @Configuration class



satishkt
Feb 8th, 2010, 10:19 PM
I was hoping that I could pass in a class annotated with @Configuration into @ContextConfiguration and extending Spring's TestNG integration class which would be able to build the context from the beans defined (using @Bean) in the Configuration class.

This would have given me the benefit of loading only the essential bean dependencies that my test bean needed, injecting mocks for the ones that I cant create in a unit test and also not having to create an xml file for individual tests (I guess I would write a seperate @Configuration java file for each test, but I could use the inheritance to easily manage those).

But the @ContextConfiguration provides options only for passing in xml(s) files only. Is there a different way to achieve what I want (I did look thru the API docs but could not find anything that seems to fit my purpose).

thanks
-Satish

Sam Brannen
Feb 9th, 2010, 03:35 AM
But the @ContextConfiguration provides options only for passing in xml(s) files only. Is there a different way to achieve what I want (I did look thru the API docs but could not find anything that seems to fit my purpose).

That's mostly correct. Actually, @ContextConfiguration accepts an array of Strings to define the application context resource locations. These do not have to represent XML files, even though that is the default and typically the case. The ContextLoader defined by the loader attribute of @ContextConfiguration is actually what is responsible for interpreting the resource locations and loading a corresponding ApplicationContext.

FYI: Spring JavaConfig (the project from which @Configuration originally came) has its own custom ContextLoader implementation which processes @Configuration classes instead of XML files. See this JIRA for further information: http://jira.springframework.org/browse/SJC-71

At the moment, however, this functionality has not yet made it into Spring core. See this JIRA for the latest information: http://jira.springframework.org/browse/SPR-6184

Hope that clears up the situation for you.

Regards,

Sam

satishkt
Feb 9th, 2010, 10:58 AM
I was about to write my own context loader on similar lines before I posted the question. I wanted to check if Spring 3.0 provided one that I could use.

Thanks Sam for clarifying.