Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Maven/Surefire finds no tests to run

As far as I can tell, the test files location is correct.

When I run "mvn test", it finds four classes named SomethingTest (they are located in the 'test' folder).

However, it ignores any of the jUnit tests (jUnit 4, annotated with @Test).

How do I debug this?

Edit - this is probablly related to wrong version of jUnit being included. I see this when running "mvn -X"

[DEBUG] Retrieving parent-POM: org.codehaus.plexus:plexus:pom:1.0.4 for project: org.codehaus.plexus:plexus-containers:pom:1.0.3 from the repository.
[DEBUG]       org.codehaus.plexus:plexus-container-default:jar:1.0-alpha-9-stable-1:runtime (selected for runtime)
[DEBUG]         junit:junit:jar:3.8.1:runtime (selected for runtime)
[DEBUG]         org.codehaus.plexus:plexus-utils:jar:1.0.4:runtime (removed - nearer found: 1.4.1)
[DEBUG]         classworlds:classworlds:jar:1.1-alpha-2:runtime (selected for runtime)

Even though my first dependency in the root pom is on jUnit 4.8.1, for some reason jUnit 3.8.1 is being included.

Edit 2 - ok, this doesn't seem to be the answer. The Test Classpath includes the correct jUnit (4) and my test classes.

Edit 3 - I had the test classes named SomethingTester. When I changed it to SomethingTest, it worked. I checked the include patterns for Surefire, and indeed it wasn't configured to catch Something Tester. Doh.

like image 476
ripper234 Avatar asked Nov 13 '10 19:11

ripper234


People also ask

How do I run a JUnit test 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 I exclude test cases in Maven?

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.


3 Answers

Maybe this is the issue:

  • https://stackoverflow.com/questions/2021771?sort=newest#sort-top

mvn -X would print a bunch of these, so you can try to figure out if it's something from the above - like not using the right JUnit version (e.g. when you create from the quickstart artifact, I think the default is 3.8.1), having TestNG in the classpath before JUnit or so.

Edit: I just tried this in a simple project and the class given in the above link and it worked fine. I used junit version 4.8, that is the only dependency in my project. Just to confirm, you are annotating test methods with @org.junit.Test and there are some org.junit.Assert.assertXXX statements in these methods, correct?

Edit 2: To change junit to some other version, use this:

<dependency>
  <groupId>junit</groupId>
  <artifactId>junit</artifactId>
  <version>4.8</version>
  <scope>test</scope>
</dependency>

Edit 3: You should have something like this in the test classpath:

[DEBUG] Test Classpath :
[DEBUG]   /home/icyrock/java/prb/target/test-classes
[DEBUG]   /home/icyrock/java/prb/target/classes
[DEBUG]   /home/icyrock/.m2/repository/junit/junit/4.8/junit-4.8.jar
[DEBUG]   /home/icyrock/.m2/repository/org/slf4j/slf4j-api/1.6.1/slf4j-api-1.6.1.jar
[DEBUG]   /home/icyrock/.m2/repository/org/slf4j/slf4j-log4j12/1.6.1/slf4j-log4j12-1.6.1.jar
[DEBUG]   /home/icyrock/.m2/repository/log4j/log4j/1.2.16/log4j-1.2.16.jar

Edit 4: OK, I just created a test project with maven quickstart artifact, added two modules (also created with quickstart artifact) inside, added source/target Java version and junit:junit:4.8 dependency to the parent pom only. I changed only one of the tests to JUnit4 (the other one is by default JUnit3, that's what quickstart generates), mvn clean test from parent folder worked just fine.

This is most likely a project setup issue - can you check your project is wired correctly (i.e. modules point to the parent, the group/artifact/versions of parent/child projects are correct). The only other thing that comes to my mind is cleaning your maven repository (at least org/apache/maven), but I doubt that would help.

It might be wise to test out on a simpler project.

like image 175
icyrock.com Avatar answered Oct 05 '22 02:10

icyrock.com


To finish icyrock.com's question. If there aren't any test classes compiled to target/test-classes then check your pom file and ensure that the packaging isn't 'pom'.

like image 36
Raymond Avatar answered Oct 05 '22 02:10

Raymond


Check pout if your current version of JUnit is still available in the Maven repository, otherwise, it would fail

The current version @time of this answer is

<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.12</version>
    <scope>test</scope>
</dependency>
like image 30
Jose Mhlanga Avatar answered Oct 05 '22 03:10

Jose Mhlanga