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

前端 未结 3 1515
梦谈多话
梦谈多话 2021-01-01 16:57

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)

相关标签:
3条回答
  • 2021-01-01 17:17

    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

    0 讨论(0)
  • 2021-01-01 17:36

    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)

    0 讨论(0)
  • 2021-01-01 17:37

    The attached test-jar can be used as a usual dependency in other project which supports reuse of code in the test area but you can't run tests out of the jar. If you really need the solution you have to write at least a single suite (etc.?) to start the tests from the jar.

    0 讨论(0)
提交回复
热议问题