Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 259
Vitaliy Avatar asked Sep 02 '12 17:09

Vitaliy


People also ask

How do I run a test case from a JAR file?

Go To Export. Select Jar or Runnable Jar as per your use click on next. Specify the main file name. Click on Finish.

Does Maven include test classes in jar?

You can produce a jar which will include your test classes and resources. To reuse this artifact in an other project, you must declare this dependency with type test-jar : <project>


2 Answers

This actually works quite fine with the newer surefire and failsafe plugins, see related questions:

  • Run JUnit Tests contained in dependency jar using Maven Surefire
  • run maven tests from classpath

So you don't need to unpack the jar anymore, you just provide the group and artifact id for the dependencies to scan (this works with both "main jar" dependencies, as well as "test-jar" dependencies)

like image 42
mac Avatar answered Oct 20 '22 12:10

mac


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

like image 191
Stephen Connolly Avatar answered Oct 20 '22 11:10

Stephen Connolly