Search code examples
unit-testingmavenjar

How to configure pom to run tests packaged in a jar?


I have a maven build process that publishes executable jars and their tests to Nexus. I have another maven build process that needs to access these jars (executable + test) and run the tests.

How do I go about it? So far I have managed to do this only if the jar is exploded to class files. I am new to maven and completely lost in the documentation.


Solution

  • Update 2022-03-11

    The feature has been implemented, see https://stackoverflow.com/a/17061755/1589700 for details

    Original answer

    Surefire and failsafe do not currently support running tests from within a jar.

    This is largely a case of not being able to identify the tests.

    There are two ways to get the tests to run.

    1. Use a test suite that lists all the tests from the test-jar. Because the test suite will be in src/test/java (more correctly will be compiled into target/test-classes) that will be picked up and all the tests in the suite will be run by Surefire/failsafe (assuming the suite class name matches the includes rule: starts or ends with Test)

    2. Use the maven dependency plugin's unpack-dependencies goal to unpack the test-jar into target/test-classes (this screams of hack, but works quite well)

    The main issue with the first option is that you cannot easily run just one test from the suite, and you need to name every test from the test-jar

    For that reason I tend to favour option 2... There is the added benefit that option 2 does not mean writing code to work around a limitation in a build tool plugin... The less you lock yourself into a specific build tool, the better IMHO