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"})
File class to read the /src/test/resources directory by calling the getAbsolutePath() method: String path = "src/test/resources"; File file = new File(path); String absolutePath = file. getAbsolutePath(); System. out.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With