【问题标题】:Surefire is not picking up Junit 4 testsSurefire 没有接受 Junit 4 测试
【发布时间】:2011-01-12 01:08:45
【问题描述】:

即使我尝试了另一篇文章的所有建议,我也无法让 Maven Surefire 执行我的 JUnit 4 测试。

我的 POM:

<project>
  <modelVersion>4.0.0</modelVersion>
  <groupId>maven-test</groupId>
  <artifactId>maven-test</artifactId>
  <version>0.0.1-SNAPSHOT</version>

  <dependencies>
    <dependency>
      <groupId>org.junit</groupId>
      <artifactId>com.springsource.org.junit</artifactId>
      <version>4.4.0</version>
      <scope>test</scope>
    </dependency>  
  </dependencies>

  <build> 
    <plugins> 
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <configuration>
          <source>1.5</source>
          <target>1.5</target>
        </configuration>
      </plugin>  
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>2.5</version>
      </plugin> 
    </plugins>
  </build> 
</project>

我的测试:

public class TestSimple {

 public void testSurePass()
 {
  int x = 1;
  Assert.assertEquals(1, x);
 }

 @Test
 public void surePass()
 {
  int x = 1;
  Assert.assertEquals(1, x);  
 }
}

Surefire 选择了 testSurePass,但从未见过 SurePass。我试过 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.5:runtime (selected for runtime)
[DEBUG]     org.apache.maven.surefire:surefire-api:jar:2.5:runtime (selected for runtime)
[DEBUG] Adding to surefire booter test classpath: C:\Users\.m2\repository\org\apache\maven\surefire\surefire-booter\2.5\surefire-booter-2.5.jar
[DEBUG] Adding to surefire booter test classpath: C:\Users\.m2\repository\org\apache\maven\surefire\surefire-api\2.5\surefire-api-2.5.jar
[DEBUG] dummy:dummy:jar:1.0 (selected for null)
[DEBUG] Retrieving parent-POM: org.apache.maven.surefire:surefire-providers:pom:2.5 for project: null:surefire-junit:jar:null from the repository.
[DEBUG] Adding managed dependencies for unknown:surefire-junit
[DEBUG]   org.apache.maven.surefire:surefire-api:jar:2.5
[DEBUG]   org.apache.maven.surefire:surefire-booter:jar:2.5
[DEBUG]   org.codehaus.plexus:plexus-utils:jar:1.5.9
[DEBUG]   org.apache.maven.surefire:surefire-junit:jar:2.5:test (selected for test)
[DEBUG]     junit:junit:jar:3.8.1:test (selected for test)
[DEBUG]     org.apache.maven.surefire:surefire-api:jar:2.5:test (selected for test)
[DEBUG] Adding to surefire test classpath: C:\Users\.m2\repository\org\apache\maven\surefire\surefire-junit\2.5\surefire-junit-2.5.jar
[DEBUG] Adding to surefire test classpath: C:\Users\.m2\repository\junit\junit\3.8.1\junit-3.8.1.jar
[DEBUG] Adding to surefire test classpath: C:\Users\.m2\repository\org\apache\maven\surefire\surefire-api\2.5\surefire-api-2.5.jar
[DEBUG] Test Classpath :
[DEBUG]   C:\Workspace\tests\maven-test\target\test-classes
[DEBUG]   C:\Workspace\tests\maven-test\target\classes
[DEBUG]   C:\Users\.m2\repository\org\junit\com.springsource.org.junit\4.4.0\com.springsource.org.junit-4.4.0.jar
[DEBUG] Setting system property [user.dir]=[C:\Workspace\tests\maven-test]
[DEBUG] Setting system property [localRepository]=[C:\Users\.m2\repository]
[DEBUG] Setting system property [basedir]=[C:\Workspace\tests\maven-test]

Surefire 2.5 自动将 junit-3.8.1 添加到其测试类路径中。虽然还添加了 junit-4.4.0(不是为了确保测试类路径,而是测试类路径),但 junit-3.8.1 似乎优先。根据http://maven.apache.org/surefire/surefire-providers/dependencies.html,Maven 的surefire-api 项目依赖于junit-3.8.1。

我在这里缺少一些配置吗?

【问题讨论】:

    标签: maven-2 junit4 surefire


    【解决方案1】:

    Surefire 有一个 junit 版本的工件检测机制,它被您使用 springsource 打包的 junit 工件所欺骗。您需要设置junitartifactname parameter 到 org.junit:com.springsource.org.junit

    【讨论】:

      【解决方案2】:

      我迷路了,我无法重现您的问题。但是确实存在一个 Jira 问题(见EBR-220),说明在使用 SpringSource 打包的 JUnit 工件时需要在surefire配置中添加以下内容:

      <junitArtifactName>org.junit:com.springsource.org.junit</junitArtifactName>
      

      【讨论】:

      • 感谢帕斯卡!由于克罗森沃尔德首先回答了相同的解决方案,因此我将支票放在他的旁边。
      • @Candy 没问题,这实际上非常合适。我刚刚发布了这个答案以供 Jira 参考。
      【解决方案3】:

      可以将 JUnit 4.x 与 maven 1.x 一起使用:

      * add Junit 4.X in your dependencies
      * Use the JUnit4TestAdapter in your test classes :
        /**
      
      * @return instance of this as Junit test case
        */
        public static junit.framework.Test suite() { return new JUnit4TestAdapter(MyTestClass.class); }
      
      * run maven with a jdk >= 5
      

      【讨论】:

        【解决方案4】:

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2020-12-31
          • 1970-01-01
          • 2016-01-04
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多