Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run Junit Suite using Maven Command

I have multiple Junit test suites (SlowTestSuite, FastTestSuite etc). I would like to run only specific suite using maven command. e.g.

mvn clean install test -Dtest=FastTestSuite -DfailIfNoTests=false 

but its not working. Just not running any test at all. Any suggestions please.

like image 504
Java SE Avatar asked Aug 01 '12 15:08

Java SE


People also ask

How do I run a JUnit project in Maven?

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 run a test suite in Maven?

The Maven surefire plugin provides a test parameter that we can use to specify test classes or methods we want to execute. If we want to execute a single test class, we can execute the command mvn test -Dtest=”TestClassName”.


2 Answers

I have achieved this by adding property into pom as:

<properties>     <runSuite>**/FastTestSuite.class</runSuite> </properties> 

and maven-surefire-plugin should be:

        <plugin>             <groupId>org.apache.maven.plugins</groupId>             <artifactId>maven-surefire-plugin</artifactId>             <configuration>                 <includes>                     <include>${runSuite}</include>                 </includes>             </configuration>         </plugin> 

so it means by default it will run FastTestSuite but you can run other test e.g. SlowTestSuite using maven command as:

mvn install -DrunSuite=**/SlowTestSuite.class -DfailIfNoTests=false 
like image 81
Java SE Avatar answered Oct 08 '22 21:10

Java SE


The keyword you missed is maven-surefire-plugin :http://maven.apache.org/plugins/maven-surefire-plugin/.

Usage is :

<project>   [...]   <build>     <plugins>       <plugin>         <groupId>org.apache.maven.plugins</groupId>         <artifactId>maven-surefire-plugin</artifactId>         <version>2.12.1</version>         <configuration>           <includes>             <include>**/com.your.packaged.Sample.java</include>           </includes>         </configuration>       </plugin>     </plugins>   </build>   [...] </project> 

If you make a little search on stack overflow, you may find information :

Running a JUnit4 Test Suite in Maven using maven-failsafe-plugin Using JUnit Categories with Maven Failsafe plugin

In addition, you may define profile, like fastTest, that will be triggered by adding parameter to cmd line :

mvn package -PfastTests 

This profile would include some inclusions too.

like image 24
Jean-Rémy Revy Avatar answered Oct 08 '22 22:10

Jean-Rémy Revy