Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding /src/main/resources to Classpath when Running Spring JUnit Tests from Eclipse

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"})
like image 648
EngineerBetter_DJ Avatar asked Mar 09 '12 10:03

EngineerBetter_DJ


People also ask

How do I get SRC test resources?

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.


1 Answers

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.

like image 87
Kkkev Avatar answered Oct 12 '22 23:10

Kkkev