Announcement Announcement Module
Collapse
No announcement yet.
Inject PropertySource in Junit tests Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Inject PropertySource in Junit tests

    I have some tests that heavily depend on SpringJUnit4ClassRunner I also Wrapped apache commons-configuration as a PropertySource, what I need is a way to inject this propertySource into the environment of the context created by SpringJUnit4ClassRunner, this same feature is available in ContextLoaderListener. Any ideas?

  • #2
    Assuming your are using tests configured using XML resources

    Create a class that extends the GenericXmlContextLoader that adds your property source to the context prior to loading the beanDefinitions.

    Code:
    public class PropertySourceContextLoader extends GenericXmlContextLoader
    {
    
    	@Override
    	protected void loadBeanDefinitions(GenericApplicationContext context,
    	        MergedContextConfiguration mergedConfig)
    	{
    		PropertySource<String> ps = new MyPropertySource();
    		context.getEnvironment().getPropertySources().addLast(ps);
    		super.loadBeanDefinitions(context, mergedConfig);
    	}
    
    }
    Specify this loader to be used by your tests.

    Code:
    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration(loader = PropertySourceContextLoader.class, locations = { "classpath:/spring-application-context.xml" })
    public class SpringBasedTest

    Comment


    • #3
      Works like a charm, thanks alot

      Comment

      Working...
      X