16

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"})
Philippe Blayo
  • 10,610
  • 14
  • 48
  • 65
DeejUK
  • 12,891
  • 19
  • 89
  • 169
  • 2
    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](http://stackoverflow.com/a/17452754/983430) for how to do it in Maven, or just add a folder to your run configuration in Eclipse. – Amos M. Carpenter Jan 20 '16 at 06:06

1 Answers1

7

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.

Kkkev
  • 4,716
  • 5
  • 27
  • 43
  • If I use `classpath*:` then it uses both sets of files, but evaluates both rather than letting the `src/test/resources` version take precedence. Is there any way around this? – DeejUK Mar 09 '12 at 15:29
  • @Deejay you should really name your stuff in src/test as {something}-test or similar, and refer to explicitly those when needed - otherwise it will never be clear if you are referring to original file or the test version – eis Apr 18 '13 at 12:57
  • @eis Thanks for your thoughts. IMO resources in src/test/resources don't need any further identification as being test resources. Additionally that adds complexity by not being able to emulate a whole-config context, as then I'd end up with both test and non-test versions of a file being loaded when using wildcards. – DeejUK Apr 18 '13 at 13:29
  • if you have something like @runwith("my-configuration.xml") annotations, which are common with (at least) spring, by looking at the code it is impossible to know if test or original configuration is referred. That is why separate names are (imo) required. – eis Apr 19 '13 at 05:37
  • 1
    @eis: Test configuration always overrides the main configuration, if the files are named the same. Hence it's simply a matter of looking in `src/test/resources/` to see what test configuration there is. – DeejUK Apr 19 '13 at 10:33