18

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.

ripper234
  • 222,824
  • 274
  • 634
  • 905

3 Answers3

6

Maybe this is the issue:

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.

Community
  • 1
  • 1
icyrock.com
  • 27,952
  • 4
  • 66
  • 85
  • mvn -X shows older jUnit versions, however it is not my poms that includes them. How do I remove it? – ripper234 Nov 13 '10 at 19:57
  • Just add junit:junit:4.8 as a dependency - i.e. as if you wanted that version. I edited the post. – icyrock.com Nov 13 '10 at 19:59
  • Retrieving is OK, what does it say in "Test Classpath" section just above "T E S T S" line? I will edit to specify what it should if configured OK. – icyrock.com Nov 13 '10 at 20:13
  • Btw, what do you mean by "root pom"? Is that the pom of the project in question or some other pom? – icyrock.com Nov 13 '10 at 20:17
  • The classpath seems right. It includes both the test and src classes, and junit 4.8.1 jar. Still, under "T E S T S", it says "There are no tests to run" – ripper234 Nov 13 '10 at 20:18
  • I setup one common pom, and another pom per module that uses the first one as a parent. – ripper234 Nov 13 '10 at 20:19
  • 3
    To confirm, your test classes are in `src/test/java` folder? Can you check there are .class files in `target/test-classes`? – icyrock.com Nov 13 '10 at 20:21
  • Test classes are in 'test' dir, but I have configured testSourceDirectory accordingly. There are .class files in target/test-classes. – ripper234 Nov 13 '10 at 20:25
4

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

Raymond
  • 406
  • 3
  • 9
1

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>
Jose Mhlanga
  • 805
  • 12
  • 14