Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run JUnit tests in Maven without building and copying files [duplicate]

Tags:

java

maven

I have a large Maven project which has several modules in it. When I want to run a JUnit test from one module I run 'mvn -Dtest=NameOfTest test' in the directory that contains all the modules. When I run this command Maven goes through each module and tries to compile it (though it's already compiled), which involves copying a bunch of files and adds to the total time of the test. It seems that the 'test' command for the Maven surefire plugin executes all the steps up to the test. I was wondering if there is a way to execute only the test step and not bother with all the attempted compilation and copying of files.

Here is some output from before the test starts:

[INFO]  [INFO] --- build-helper-maven-plugin:1.5:add-test-source (add-test-source) @ module1 --- [INFO] Test Source directory: <directory in module1 with some generated sources> added. [INFO]  [INFO] --- maven-resources-plugin:2.5:testResources (default-testResources) @ module1 --- [debug] execute contextualize [INFO] Copying 108 resources [INFO] Copying 1113 resources [INFO] Copying 1 resource [INFO] 

It repeats this for each of the other modules. All told it takes a minute or two before it actually starts the test. Does anyone know a way to get the test to run without bothering with all the compilation beforehand? Please let me know if there's any more information I should provide.

like image 438
Ian Avatar asked Feb 14 '13 18:02

Ian


People also ask

How do I run a Maven test only?

If you want to run just a single test instead of all the tests declared in your project, create a Maven run configuration for a single test with the Maven -Dtest=TestName test command. The run configuration will be saved under the Run Configurations node.

How do you run a JUnit test case in Maven build?

We can run our unit tests with Maven by using the command: mvn clean test. When we run this command at command prompt, we should see that the Maven Surefire Plugin runs our unit tests. We can now create a Maven project that compiles and runs unit tests which use JUnit 5.

How do you ignore test cases in Maven build?

To skip running the tests for a particular project, set the skipTests property to true. You can also skip the tests via the command line by executing the following command: mvn install -DskipTests.

Does mvn clean install run tests?

Running 'mvn clean install' will run the default build. As specified above integration tests will be ignored. Running 'mvn clean install -P integration-tests' will include the integration tests (I also ignore my staging integration tests).


2 Answers

If what you would like to do is to run just the test phase of the lifecycle without running all of the previous phases you could also call the goal that is bound to the test phase:

mvn surefire:test 

or if you want to run just one test

mvn -Dtest=NameOfTest surefire:test 
like image 173
Akro Avatar answered Sep 25 '22 20:09

Akro


What is wrong with simply running the test from within the module the test resides in? That way, Maven will not attempt to build the other modules which you are not interested in.

like image 43
JamesB Avatar answered Sep 21 '22 20:09

JamesB