java - Unable to get Maven (and Eclipse) to use test resources while testing -


i having trouble getting unit tests use resources provided under src\test\resources instead of in src\main\resources. yes, resource in question named identically in both places.

i have done fair amount of research, looked @ stackoverflow posts such as:

however basic issue has me stumped.

i have standard maven java project set up:

  • application source & resources under src\main
  • test source & resources under src\test

eclipse (thanks m2eclipse plugin) has src\main\java, src\main\resources, src\test\java , src\test\resources in build path.

with setup, when run unit test within eclipse resource files in src\main\resources being referenced. can see why (because in build path) don't know how prevent it. unit test configuration panel not allow me tweak order of (default) classpath components.

even if gloss on inability run tests eclipse, maven doesn't play nice either. can see processresources step copying test resources under target\test-classes tree. when unit test executed copy of resource file under target\classes being consulted.

surely overlooking obvious. appreciate can provide.

[edit] adding more (possibly relevant information):

the resource file in question file named persistence.xml. used eclipselink connect relational data sources. version of file used application placed in src\main\resources\meta-inf. maven build (of war, say) places meta-inf directory under classes directory. therefore eclipselink tooling finds file.

src/main   |   + -- java/com/company/productdao.java   |    + -- resources/meta-inf/persistence.xml  (requires application container)   | src/test   |   + -- java/com/company/productdaotest.java   |    + -- resources/meta-inf/persistence.xml  (out of container unit tests)   | 

the takeaway here is: eclipselink requires file called persistence.xml in meta-inf directory on classpath (yes, defaults can overridden let's not go there).

therefore when unit test runs same application logic invoked. version of file in src\main\resources cannot used. different version made available in src\test\resources directory.

since version in src\main\resources being used appears recourse instruct eclipselink during unit testing different filename.

this isn't maven issue @ root of it. you'd have exact same problem if had collapsed single source folder. think doing. unit tests require code src/main or else wouldn't unit test of code. src/main/java , src/main/resources make artifact testing. don't mix , match directories unit test.

you doing this:

/src/com/mycompany/myresource.txt         /src/com/mycompany/myawesomeclass.java /src/com/mycompany/myresource.txt /src/com/mycompany/testmyawesomeclass.java 

don't that. configure unit tests load mock data / resource file file.

i haven't had many, if issues maven , eclipse. realize maven not magic. , if going use maven , eclipse has driven pom file not other way around. things work beautifully.

just reclarify, issue has nothing maven. declaring resource twice on classpath. need rethink unit tests. there things can around profiles it's abuse of build system.


Comments

Popular posts from this blog

c# - SVN Error : "svnadmin: E205000: Too many arguments" -

c++ - Using OpenSSL in a multi-threaded application -

All overlapping substrings matching a java regex -