Java – Add src/main/resources to classpath when running unit test in eclipse for spring

eclipsejavamavenspring

In eclipse my maven project has a 'src/main/resources/' folder and a 'src/test/resources' folder. The contents of both directories seem to go into 'Project/target/classes', and the content of just the 'src/test/resources' directory goes into 'Project/target/test-classes'.

When I use spring to load a classpath resource whilst testing, it seems to only have available the stuff that's in 'test-classes' folder on the classpath. This makes sense, as only testing resources go in the classpath for testing.

However I want my main resources to be available when running unit tests aswell, but they cannot be found when running unit tests as they are not on the classpath. How do I configure my way around this problem?

Best Answer

You can configure the surefire plugin (which is what maven uses to run unit tests) to include any additional classes, jars or resource directories.

http://maven.apache.org/plugins/maven-surefire-plugin/examples/configuring-classpath.html