Surefire没有接受Junit 4测试

时间:2010-01-07 16:15:52

标签: java maven-2 maven surefire

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

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

但是,如果我将此类更改为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");
  }
}

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

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

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

还有什么我想念的吗?

13 个答案:

答案 0 :(得分:30)

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

所以似乎问题来自testng jar需要JUnit v3.8.1。即使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>

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

经验教训:

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

感谢大家的帮助。不幸的是,没有办法在Pascal和Kaleb之间分割答案点,但是K​​aleb建议使用mvn -X帮助我走上正确的轨道,所以正确的答案点就在他身上。

答案 1 :(得分:21)

Surefire插件根据类路径确定应该使用哪个JUnit提供程序。如果类路径上有多个JUnit版本,则可以更正类路径,使类路径上只有一个JUnit版本(如上所述),或者可以明确指定要使用的提供程序。例如,使用最新的提供程序(例如,“surefire-junit47”)在您的(父)POM中指定以下内容:

[...]
<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>
[...]

但请注意,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>
[...]

答案 2 :(得分: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>

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

答案 3 :(得分:9)

另一个可能原因可能是此错误(以“无法修复”结束):https://issues.apache.org/jira/browse/SUREFIRE-587

简短摘要:如果名称不以“Test”结尾,则不会选择扩展TestCase(但不使用注释)的测试。

答案 4 :(得分:4)

为了Google员工的利益,当我遇到这个问题时,原因是我在PowerNock中包含了一个引入TestNG的依赖项,导致SureFire没有检测到[TestNG]测试。

我使用POM编辑器的m2eclipse“Dependency Hierarchy”选项卡查找依赖项并右键单击以生成排除项(请参阅下面的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>

(在我的情况下,排除“powermock-module-testng”就足够了,但如果它从其他地方进来,你可以直接排除TestNG。)

答案 5 :(得分:4)

对于那些想知道为什么Maven没有接受JUnit测试的可怜的灵魂。

我将JUnit和TestNG都作为依赖。但我希望使用TestNG运行我的功能测试,并使用sureUnun来运行我的单元测试。

然而,我发现surefire试图使用TestNG运行我的单元测试,但没有发现任何东西可以运行。我的JUnit测试被跳过了。

后来我遇到了这个Maven issue并配置了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>

希望这有助于某人。

答案 6 :(得分:1)

您所做的验证很好,尤其是检查您使用的是版本2.3+的surefire插件(默认情况下,您将获得版本2.4.3和maven 2.1 super POM所以这应该是确定)并检查您是否传递了junit-3.8.1.jar依赖关系。

现在,只是为了验证这不是一个“全局问题”(我不这么认为是TBH),你能否从头开始创建一个项目,例如运行:

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

然后更新junit依赖:

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

并将编译器级别配置为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>

最后将SimpleTest.java放在AppTest.java旁边并运行mvn test

如果运行mvn test对该项目运行正常(我希望它能正常运行),您能否使用您正在使用的POM配置更新您的问题(来自项目有问题)?

答案 7 :(得分:1)

一个小小的改变帮助我Funnily !!!

我将类名从MyClassTest更改为TestMyClass, 我的父POM.xml包含在下面的行

后,我得到了这个想法
<test.include.pattern> **/Test*.java <test.include.pattern/>

答案 8 :(得分:1)

1。)在pom.xml中包含以下surefire插件

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

2.)默认情况下,surefire插件从包中选择Test类: - src / test / java /....

所以转到Build Path并在classpath中包含test文件夹,如下所示:

enter image description here enter image description here

3。)转到 - &gt;运行方式 - &gt; Maven测试

[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

答案 9 :(得分:1)

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

<dependency>
  <groupId>org.junit.vintage</groupId>
  <artifactId>junit-vintage-engine</artifactId>
  <scope>test</scope>
</dependency>

答案 10 :(得分:0)

您是否为正确的编译器级别配置了maven-compile-plugin,例如:

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

否则maven会出现注释问题

答案 11 :(得分:0)

尝试运行集成测试时遇到类似问题。我有一个旧版本的surefire插件试图运行TestNG而不是jUnit。我将pom中的版本号更改为2.20并且有效。

答案 12 :(得分:0)

如果您使用的是Maven,则文件名很重要

默认情况下,测试文件名必须以“ Test”或“ Tests”开头或结尾,否则在搜索测试时将被忽略。

https://maven.apache.org/surefire/maven-surefire-plugin/examples/inclusion-exclusion.html

相关问题