Search code examples
javamavenunit-testingmaven-surefire-plugin

Run an already compiled unit test with Maven


I would like to know if it is possible to run a previously compiled test (using mvn test-compile) without having to recompile a project and its tests. I'm aware of the mvn -Dtest=TestClass#testMethod test command, but it does not fit my need.

More generally I'm trying to find a way to run unit tests is a specific order using a list of test names. Any idea would be appreciated.


Solution

  • If you start maven by calling a phase it will execute all lifecycle phases up to the one you are calling. For example, when calling

    mvn test
    

    all the phases before the test lifecycle phase will be execute too: the project will be validated, sources and resources will be generated and processed, sources will be compiled, the same will happen to test sources and resources and finally unit tests will be run.

    But you can also call the plugin goal that is bound to a lifecycle phase. In the case of the test phase the bound goal is surefire's test mojo. So you could call

    mvn surefire:test
    

    and no other lifecycle phase will be executed.