Alex Rivera | Logout

Surefire is not picking up Junit 4 tests

Asked 2010-01-07T16:15:52.447
47

For some reason I cannot get Maven 2 Surefire plugin to execute JUnit 4 test class.

public class SimpleTest {
  @org.junit.Test
  public void simple() {
     System.out.println("foo");
  }
}

However if I change this class to be JUnit-3 like, such as

public class SimpleTest extends junit.framework.TestCase {
  public void testBar() {
     System.out.println("bar");
  }

  @org.junit.Test
  public void simple() {
     System.out.println("foo");
  }
}

then it gets executed. Here's what I've done:

  • verified Maven version: Apache Maven 2.2.1 (r801777; 2009-08-06 20:16:01+0100)
  • verified Surefire version: followed this advice
  • verified Surefire version: checked Surefire jars in my ~/.m2/repository/org/apache/maven/surefire -- all of them are either version 2.4.2 or 2.4.3
  • done a mvn dependency:tree | grep junit to ensure I only depend on junit version 4.7

The module I am having this problem at doesn't have JUnit 3 tests.

Is there anything else I am missing?

Edit
Report

1 Answer

1

The verification that you've done are good, especially checking that you are using version 2.3+ of the surefire plugin (by default, you'll get version 2.4.3 with maven 2.1 super POM so this should be ok) and checking that you not pulling the junit-3.8.1.jar dependency transitively.

Now, just to validated that this is not a "global problem" (I don't think so TBH), could you create a project from scratch, for example by running:

mvn archetype:create -DgroupId=com.mycompany.app -DartifactId=maven-junit4-testcase

Then update the junit dependency:

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

And configure the compiler level for 1.5+

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-compiler-plugin</artifactId>
  <configuration>
    <source>1.5</source>
    <target>1.5</target>
  </configuration>
</plugin>

Finally put your SimpleTest.java next to AppTest.java and run mvn test.

If running mvn test works fine for that project (and I'm expecting it to run without problem), could you please update your question with the POM configuration you're using (from the project having troubles)?

answered 2010-01-07T17:12:44.527

Your Answer