Surefire没有接受Junit 4测试

min*_*das 35 java maven-2 surefire maven

出于某种原因,我无法获得Maven 2 Surefire插件来执行JUnit 4测试类.

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

但是,如果我将此类更改为JUnit-3,例如

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

  @org.junit.Test
  public void simple() {
     System.out.println("foo");
  }
}
Run Code Online (Sandbox Code Playgroud)

然后它被执行.这就是我所做的:

  • 已验证的Maven版本:Apache Maven 2.2.1(r801777; 2009-08-06 20:16:01 + 0100)
  • 经过验证的Surefire版本:遵循建议
  • 经过验证的Surefire版本:检查了我的Surefire罐子~/.m2/repository/org/apache/maven/surefire- 所有这些都是版本2.4.2或2.4.3
  • 做了一个mvn dependency:tree | grep junit以确保我只依赖junit 4.7版

我遇到此问题的模块没有JUnit 3测试.

还有什么我想念的吗?

min*_*das 31

mvn -X 帮我揭示了以下几点:

...
[INFO] [surefire:test {execution: default-test}]
[DEBUG] dummy:dummy:jar:1.0 (selected for null)
[DEBUG]   org.apache.maven.surefire:surefire-booter:jar:2.4.3:runtime (selected for runtime)
[DEBUG]     org.apache.maven.surefire:surefire-api:jar:2.4.3:runtime (selected for runtime)
[DEBUG] Adding to surefire booter test classpath: /home/mindas/.m2/repository/org/apache/maven/surefire/surefire-booter/2.4.3/surefire-booter-2.4.3.jar
[DEBUG] Adding to surefire booter test classpath: /home/mindas/.m2/repository/org/apache/maven/surefire/surefire-api/2.4.3/surefire-api-2.4.3.jar
[DEBUG] dummy:dummy:jar:1.0 (selected for null)
[DEBUG]   org.testng:testng:jar:jdk15:5.8:test (selected for test)
[DEBUG]     junit:junit:jar:3.8.1:test (selected for test)
[DEBUG] Adding to surefire booter test classpath: /home/mindas/.m2/repository/org/testng/testng/5.8/testng-5.8-jdk15.jar
[DEBUG] Adding to surefire booter test classpath: /home/mindas/.m2/repository/junit/junit/3.8.1/junit-3.8.1.jar
[DEBUG] dummy:dummy:jar:1.0 (selected for null)
[DEBUG] Retrieving parent-POM: org.apache.maven.surefire:surefire-providers:pom:2.4.3 for project: null:surefire-testng:jar:null from the repository.
[DEBUG] Adding managed dependencies for unknown:surefire-testng
[DEBUG]   org.apache.maven.surefire:surefire-api:jar:2.4.3
[DEBUG]   org.apache.maven.surefire:surefire-booter:jar:2.4.3
[DEBUG]   org.codehaus.plexus:plexus-utils:jar:1.5.1
[DEBUG]   org.apache.maven.surefire:surefire-testng:jar:2.4.3:test (selected for test)
[DEBUG]     org.apache.maven:maven-artifact:jar:2.0:test (selected for test)
[DEBUG]       org.codehaus.plexus:plexus-utils:jar:1.0.4:test (selected for test)
[DEBUG]     junit:junit:jar:3.8.1:test (selected for test)
[DEBUG]     org.testng:testng:jar:jdk15:5.7:test (selected for test)
[DEBUG]     org.apache.maven.surefire:surefire-api:jar:2.4.3:test (selected for test)
...
[DEBUG] Test Classpath :
...
[DEBUG]   /home/mindas/.m2/repository/junit/junit/4.7/junit-4.7.jar
Run Code Online (Sandbox Code Playgroud)

所以似乎问题来自testng需要JUnit v3.8.1的jar.尽管Test Classpath设置依赖于JUnit 4,但为时已晚.

testng 依赖位于我的POM中:

<dependency>
  <groupId>org.testng</groupId>
  <artifactId>testng</artifactId>
  <version>5.8</version>
  <scope>test</scope>
  <classifier>jdk15</classifier>
</dependency>
Run Code Online (Sandbox Code Playgroud)

在我评论完之后,测试立即开始执行.

得到教训:

  • mvn dependency:tree并不总是,mvn -X是朋友.
  • surefire不是为开发者天堂做的(我在查看项目JIRA报告时已经意识到这一点).这尤其正确,因为如果您使用Maven,则没有其他选择.

谢谢大家的帮助.不幸的是,没有办法在Pascal和Kaleb之间分开答案点,但Kaleb的使用建议mvn -X帮助我走上了正确的轨道,所以正确的答案点归于他.

  • 实际上,您应该接受自己的答案,因为它为您的问题提供了完整的解决方案:) (2认同)

Ran*_*uch 21

Surefire插件根据类路径确定应该使用哪个JUnit提供程序.如果类路径上有多个JUnit版本,您可以更正类路径,在类路径上只有一个JUnit版本(如上所述),或者您可以明确指定要使用的提供程序.例如,在您的(父)POM中使用最新的提供程序指定以下内容(例如,"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>
[...]
Run Code Online (Sandbox Code Playgroud)

但请注意,Surefire 2.7改变了确定运行哪些单元测试类的方式.将Surefire 2.7(或更高版本)与JUnit 4一起使用时的新行为是,任何没有@Test注释的测试都将被自动跳过.如果您只进行JUnit 4单元测试,这可能会很棒,但如果您有JUnit 3和4单元测试的组合,使用"surefire-junit47"提供程序将无法正常工作.在这种情况下,最好明确选择"surefire-junit4"提供者:

[...]
<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>
[...]
Run Code Online (Sandbox Code Playgroud)


ana*_*bek 13

如果您import org.junit.jupiter.api.Test;的依赖项中有 JUnit 5 ( ) 并且您正在尝试运行一些 JUnit4 ( import org.junit.Test;) 测试,请确保包含以下依赖项

<dependency>
  <groupId>org.junit.vintage</groupId>
  <artifactId>junit-vintage-engine</artifactId>
  <scope>test</scope>
</dependency>
Run Code Online (Sandbox Code Playgroud)


Kal*_*son 12

我不知道你的意思是"无法执行",但它是否有助于明确设置所使用的包含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>
Run Code Online (Sandbox Code Playgroud)

另外,运行带有-X标志的maven是否提供了任何有用的信息?


Gáb*_*ták 10

另一个可能的原因可能是这个错误(以"不会修复"结束):https://issues.apache.org/jira/browse/SUREFIRE-587

简短摘要:如果名称不以"Test"结尾,则不会获取扩展TestCase(但不使用注释)的测试.


Jam*_*mar 6

对于那里的一些可怜的人,他们想知道为什么Maven不接受JUnit测试。

我同时依赖JUnit和TestNG。但是我希望故障保护功能使用TestNG运行我的功能测试,并确保surefire使用JUnit运行我的单元测试。

但是,我发现surefire尝试使用TestNG运行我的单元测试,但没有发现要运行的内容。我的JUnit测试被跳过了。

后来我遇到了这个Maven问题,并配置surefire仅运行“ JUnit”测试,如下所示:

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
  <configuration>
    <properties>
      <property>
        <name>junit</name>
        <value>true</value>
      </property>
    </properties>
  </configuration>
</plugin>
Run Code Online (Sandbox Code Playgroud)

希望这对某人有帮助。


Dav*_*oni 5

为了Google员工的利益,当我遇到这个问题时,是因为我包含了一个PowerMock依赖关系,该依赖关系引入了TestNG,导致SureFire无法检测到[TestNG]测试。

我使用了POM编辑器的m2eclipse“依赖关系层次结构”选项卡来找到依赖关系,然后右键单击以生成排除项(请参见下面的XML)。

为了完整起见(对于那些不使用m2eclipse的人),这里是排除依赖项的XML-我只是通过查看自动生成的这些标签来了解Maven的此功能的:

<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>
Run Code Online (Sandbox Code Playgroud)

(就我而言,排除“ powermock-module-testng”就足够了,但是如果它是从其他地方传入的,则可以直接排除它。)