Surefire is not picking up Junit 4 tests

2019-01-13 17:37发布

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?

11条回答
你好瞎i
2楼-- · 2019-01-13 18:00

I don't know what you mean by "can't execute," but does it help to explicitly set the includes used by the maven-surefire-plugin?

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.4.3</version>
    <configuration>
        <includes>
            <include>**/*Test.java</include>
        </includes>
    </configuration>
</plugin>

Also, does running maven with the -X flag provide any useful information?

查看更多
我只想做你的唯一
3楼-- · 2019-01-13 18:00

For the benefit of Googlers, when I had this issue it was because I'd included a PowerMock dependency that pulled in TestNG, causing no [TestNG] tests to be detected by SureFire.

I used the m2eclipse "Dependency Hierarchy" tab of the POM editor to find the dependency and right-clicked to generate an exclusion (see XML below).

For completeness (and for those not using m2eclipse) here's the XML that excludes the dependency - I only came across this feature of Maven by seeing these tags generated automatically:

<dependency>
  <groupId>org.powermock</groupId>
  <artifactId>powermock-mockito-release-full</artifactId>
  <version>1.4.9</version>
  <classifier>full</classifier>
  <exclusions>
    <exclusion>
      <artifactId>powermock-module-testng</artifactId>
      <groupId>org.powermock</groupId>
    </exclusion>
  </exclusions>
</dependency>

(In my case, excluding "powermock-module-testng" was sufficient, but you could exclude TestNG directly if it's coming in from somewhere else.)

查看更多
迷人小祖宗
4楼-- · 2019-01-13 18:03

1.) Include following surefire plugin in pom.xml

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.20</version>
<configuration>
</configuration>
</plugin>

2.)By default surefire plugin picks Test class from package :- src/test/java/....

So go to Build Path and include test folder in classpath like below :

enter image description here enter image description here

3.) Go to --> Run As -->Maven Test

[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 1.081 
s - in com.bnym.dcm.api.controller.AccountControllerTest
[INFO] Running com.bnym.dcm.api.controller.DCMApiControllerTest
[INFO] Tests run: 6, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0 s - 
in com.bnym.dcm.api.controller.DCMApiControllerTest
[INFO] 
[INFO] Results:
[INFO] 
[INFO] Tests run: 7, Failures: 0, Errors: 0, Skipped: 0
[INFO] 
[INFO] ----------------------------------------------------------------------
--
[INFO] BUILD SUCCESS
查看更多
Rolldiameter
5楼-- · 2019-01-13 18:07

Have you configured your maven-compile-plugin for the correct compiler level, like:

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

Otherwise maven will have trouble with annotations

查看更多
走好不送
6楼-- · 2019-01-13 18:11

The Surefire plugin figures out which JUnit provider should be used based upon the classpath. If there are multiple JUnit versions on the classpath, you can either correct the classpath to have only one JUnit version on the classpath (as discussed above), or you can explicitly specify which provider you want to use. For example, specifying the following in your (parent) POM forces using the newest provider (e.g., "surefire-junit47"):

[...]
<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-surefire-plugin</artifactId>
  <version>2.8</version>
  <dependencies>
    <!-- Force using the latest JUnit 47 provider -->
    <dependency>
      <groupId>org.apache.maven.surefire</groupId>
      <artifactId>surefire-junit47</artifactId>
      <version>2.8</version>
    </dependency>
  </dependencies>
[...]

Note however that Surefire 2.7 changed the way it's determining which unit test classes are run. The new behavior when using Surefire 2.7 (or later) with JUnit 4 is that any test without a @Test annotation will be skipped automatically. This may be great if you just have JUnit 4 unit tests, but if you have a combination of JUnit 3 and 4 unit tests, using the "surefire-junit47" provider will not work correctly. In such cases, its best to explicitly choose the "surefire-junit4" provider:

[...]
<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-surefire-plugin</artifactId>
  <version>2.8</version>
  <dependencies>
    <dependency>
      <groupId>org.apache.maven.surefire</groupId>
      <!-- Use the older JUnit 4 provider -->
      <artifactId>surefire-junit4</artifactId>
      <version>2.8</version>
    </dependency>
  </dependencies>
[...]
查看更多
叼着烟拽天下
7楼-- · 2019-01-13 18:16

One more possible cause can be this bug (closed with "Won't fix"): https://issues.apache.org/jira/browse/SUREFIRE-587

Short summary: Tests extending TestCase (but not using annotations) will not be picked up if their name does not end with "Test".

查看更多
登录 后发表回答