16
votes

Is there a way of getting JUnit tests in Eclipse (specifically I'm using SpringJUnit4ClassRunner) to use resources from src/main/resources as well as src/test/resources?

Maven's surefire plugin does this, but running one particular unit test from Eclipse does not.

I've got a load of Spring config in src/main/resources/spring-config/ and I want to 'override' two specific files. I've placed these test-specific overrides in src/test/resources/spring-config/, and when running unit tests through Maven eveerything works. When I run a JUnit test from Eclipse, only the test files are available, and unsurprisingly my context fails to load.

Update

Is appears the Spring classes that configure the test context can't find the resources from src/main/resources/ when using a wildcard, but will find them if I specify them explicitly.

Doesn't find things from src/main/resources/

@ContextConfiguration(locations = {"classpath:/spring-config/*.xml"})

Does find specified files from src/main/resources/

@ContextConfiguration(locations = {"classpath:/spring-config/*.xml",
                                   "classpath:/spring-config/app-aws.xml"})
1
Just to point out to anyone wanting to answer the question, "Is there a way of getting JUnit tests in Eclipse/Maven to use resources from src/main/resources as well as src/test/resources?" (and not so interested in the Spring part), see this answer for how to do it in Maven, or just add a folder to your run configuration in Eclipse.Amos M. Carpenter

1 Answers

7
votes

This is actually a limitation of Spring, rather than JUnit. See http://static.springsource.org/spring/docs/3.1.x/spring-framework-reference/html/resources.html#resources-wildcards-in-path-other-stuff for details.

The solution is to place your Spring config files in a package rather than in the root package.