Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Surefire doesn't launch test in src/main/java

Tags:

I needed to move some src/test/java to src/main/java according to this recommandation from maven-jar-plugin documentation => http://maven.apache.org/plugins/maven-jar-plugin/usage.html

I did so because, i used tests (helper) classes in another projects in test scope.

So i create my-project-test, moved in test classes, and configured surefire to specify test classes directory =>

<build>     <plugins>         <plugin>             <groupId>org.apache.maven.plugins</groupId>             <artifactId>maven-surefire-plugin</artifactId>             <configuration>                 <testSourceDirectory>${basedir}\src\main\java\</testSourceDirectory>             </configuration>         </plugin>     </plugins> </build> 

But when i launched mvn test, 0 tests is executed =>

Results :  Tests run: 0, Failures: 0, Errors: 0, Skipped: 0 

The problem is the same as this one => Maven does not find JUnit tests to run but the solution (configure surefire maven plugin) is not working for me.

like image 619
Antoine Avatar asked Jan 24 '12 12:01

Antoine


People also ask

What is the difference between SRC main java and SRC test java?

src/main/java places your code that use for real production. src/test/java places your test use case code, like junit test. These codes would be executed when doing maven package things.

Why your JUnit 5 tests are not running under Maven?

Prior to these releases, to run JUnit 5 tests under Maven, you needed to include a JUnit provider dependency for the Maven Surefire plugin. This is correct for pre 2.22. 0 releases of Maven Surefire/Failsafe.

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.

Which Maven plugin triggers the test case?

Work Flow Using The Maven Surefire Plugin As TestNG has the capability to select and execute particular test scripts or suites, we are integrating this with Maven using the Maven Surefire plugin.


2 Answers

The good configuration for maven surefire plugin is =>

<build>         <plugins>             <plugin>                 <groupId>org.apache.maven.plugins</groupId>                 <artifactId>maven-surefire-plugin</artifactId>                 <configuration>                     <testSourceDirectory>${basedir}/src/main/java/</testSourceDirectory>                     <testClassesDirectory>${project.build.directory}/classes/</testClassesDirectory>                 </configuration>             </plugin>         </plugins>     </build>  Results :  Tests run: 20, Failures: 0, Errors: 0, Skipped: 0 
like image 84
Antoine Avatar answered Dec 09 '22 01:12

Antoine


Since the test classes have now been placed in src/main/java, it is likely that they are getting compiled during the compile phase to target/classes.

So, though surefire is configured to indicate that the test source location is src/main/java, it would still look for test classes in target/test-classes, since this is the default location for test classes.

You could try specify testClassesDirectory parameter suitably and see if it works.

Also, I think you may have misinterpreted the recommendation in the maven jar plugin documentation. I do not believe they suggest moving test sources to src/main/java.

like image 24
Raghuram Avatar answered Dec 09 '22 03:12

Raghuram