views:

18

answers:

2

In order to clean up something of a giant mess, I set out to put the code of my tests all in one ordinary java project (all in src/main/java), and then declare that as a <scope>test</scope> dependency in another project, and expect the tests to run.

No such luck. surefire wants to just run the tests that it can see in the sources.

I can see a sadly obvious solution here involving the build-helper-plugin and adding the tests into the test compilation environment as a source directory, but I was hoping to avoid it.

In case anyone is wondering, the reason for all this is that the POM configuration for use of the failsafe plugin to run some integration tests got so complex that I wanted to split out the compiling of the test classes from the running of the tests.

+1  A: 

Can't you do it the other way round?

I mean put the code the src/test/java, depend on your main module, and run the tests in your test module?

Peter Tillemans
+2  A: 

No such luck. surefire wants to just run the tests that it can see in the sources.

This is currently not possible out of the box, surefire just looks at classes in target/test-classes:

This is actually logged as SUREFIRE-569 - There should be a way to run unit tests from a dependency jar.

I can see a sadly obvious solution here involving the build-helper-plugin and adding the tests into the test compilation environment as a source directory, but I was hoping to avoid it.

The current workaround is to use dependency:unpack to unpack the jar into target/test-classes before the test phase.

Pascal Thivent
@Pascal: when I do this, I get NoClassDefFound errors for org.junit.Assert! But this may yet be some prank of something else.
bmargulies
@bmargulies Hmm... weird. I fail to understand why you would get that NoClassDefFoundError. I might try this, but later.
Pascal Thivent
@Pascal I filed a bug report. I have some ideas, but I made it go away by seemingly unrelated tweaks to the classpath. It *might* have results from Eclipse writing a class file at the wrong moment.
bmargulies
@bmargulies: do you have a dependency to junit in the artifact that executes the tests? (not the artifact you are unpacking)
seanizer
@sean yes I did when I had the problem, and -X showed it in the classpath. However, the error message eventually suggested that it was, in fact, crazily looking for 'Assert' and not 'org.junit.Assert', so I went on a cleaning jihad and things got better.
bmargulies