Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Maven doesn't execute any unit test

I am using Maven with multi-modules. There are 3 projects.

foo(the parent project) foo-core foo-bar 

I configure all the dependencies and plugins in foo's pom:

<modules>     <module>../foo-core</module>     <module>../foo-bar</module> </modules>  <dependencyManagement>     <dependencies>         <dependency>             <groupId>junit</groupId>             <artifactId>junit</artifactId>             <version>4.11</version>             <scope>test</scope>         </dependency>         <dependency>             ...         </dependency>     </dependencies> </dependencyManagement>  <build>     <pluginManagement>         <plugins>             <plugin>                 <groupId>org.apache.maven.plugins</groupId>                 <artifactId>maven-compiler-plugin</artifactId>                 <version>2.3.1</version>                 <configuration>                     <source>1.6</source>                     <target>1.6</target>                 </configuration>             </plugin>             <plugin>                 <groupId>org.apache.maven.plugins</groupId>                 <artifactId>maven-surefire-plugin</artifactId>                 <version>2.14.1</version>                 <dependencies>                     <dependency>                     <groupId>org.apache.maven.surefire</groupId>                     <artifactId>surefire-junit47</artifactId>                     <version>2.14.1</version>                     </dependency>                 </dependencies>             </plugin>         </plugins>     </pluginManagement> </build> 

There are several base classes and util classes for unit test in foo-core, so I add the maven-jar-plugin in foo-core project to make it available to foo-bar:

<build>     <plugins>         <plugin>             <groupId>org.apache.maven.plugins</groupId>             <artifactId>maven-jar-plugin</artifactId>             <version>2.3.1</version>             <executions>                <execution>                    <goals>                        <goal>test-jar</goal>                    </goals>                </execution>             </executions>         </plugin>     </plugins> </build> 

When I execute test goal, I got the result as follow:

-------------------------------------------------------  T E S T S ------------------------------------------------------- parallel='none', perCoreThreadCount=true, threadCount=2, useUnlimitedThreads=false  Results :  Tests run: 0, Failures: 0, Errors: 0, Skipped: 0 

I do got tests in my projects. But why it doesn't run any of them?

like image 932
Kirin Yao Avatar asked May 23 '13 07:05

Kirin Yao


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 Maven execute test cases?

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”.

Does Maven support JUnit?

The JUnit Platform Provider supports the test JVM system property supported by the Maven Surefire Plugin.

How do I run a JUnit test in Maven project in Eclipse?

you can run Junit 4 with Maven. You just need the Junit 4 dependency in your pom. You also need the surefire plugin to execute the tests. Hint: By default surefire looks for files with *Test.


2 Answers

Rename test files from **Tests.java to **Test.java or add the following configuration to pom.xml

<plugin>   <groupId>org.apache.maven.plugins</groupId>   <artifactId>maven-surefire-plugin</artifactId>   <version>2.14.1</version>   <configuration>     <includes>       <include>**/*Tests.java</include>     </includes>   </configuration> </plugin> 
like image 103
Grzegorz Żur Avatar answered Sep 22 '22 17:09

Grzegorz Żur


Additionally, one more cause for such a behaviour is if you have an exclusion for junit-vintage-engine in your pom.xml dependencies.

In my case I had excluded it from the spring-boot-starter-test dependency in my pom.xml

<dependency>   <groupId>org.springframework.boot</groupId>   <artifactId>spring-boot-starter-test</artifactId>   <scope>test</scope>   <exclusions>     <exclusion>       <groupId>org.junit.vintage</groupId>       <artifactId>junit-vintage-engine</artifactId>     </exclusion>   </exclusions> </dependency> 

This resulted in the same problem. Maven not able to recognize/find any test cases in the project even though they are present.

So just remove the exclusions section and run the maven command again.

like image 34
Dishant Kamble Avatar answered Sep 21 '22 17:09

Dishant Kamble