首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Surefire不会选择Junit 4测试

Surefire不会选择Junit 4测试
EN

Stack Overflow用户
提问于 2010-02-02 15:07:13
回答 4查看 7.5K关注 0票数 0

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

我的POM:

代码语言:javascript
复制
<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>

我的测试:

代码语言:javascript
复制
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:

代码语言:javascript
复制
[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。

我是不是遗漏了一些配置?

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2010-02-02 18:14:23

Surefire有一个junit版本的工件检测机制,你可以使用springsource打包的junit工件来欺骗它。您需要将junitartifactname parameter设置为org.junit:com.springsource.org.junit

票数 3
EN

Stack Overflow用户

发布于 2010-02-02 18:15:11

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

代码语言:javascript
复制
<junitArtifactName>org.junit:com.springsource.org.junit</junitArtifactName>
票数 3
EN

Stack Overflow用户

发布于 2010-08-20 14:53:37

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

代码语言:javascript
复制
* 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
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/2182376

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档