Java Surefire 不接受 Junit 4 测试

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2021771/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 02:40:49  来源:igfitidea点击:

Surefire is not picking up Junit 4 tests

javamaven-2mavensurefire

提问by mindas

For some reason I cannot get Maven 2 Surefire plugin to execute JUnit 4 test class.

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

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

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

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 thisadvice
  • 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 junitto ensure I only depend on junit version 4.7
  • 经验证的 Maven 版本:Apache Maven 2.2.1 (r801777; 2009-08-06 20:16:01+0100)
  • 经验证的 Surefire 版本:遵循建议
  • 验证的 Surefire 版本:在我的检查 Surefire jars ~/.m2/repository/org/apache/maven/surefire- 它们都是 2.4.2 或 2.4.3 版本
  • 做了一个mvn dependency:tree | grep junit以确保我只依赖于 junit 版本 4.7

The module I am having this problem at doesn't have JUnit 3 tests.

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

Is there anything else I am missing?

还有什么我想念的吗?

采纳答案by mindas

mvn -Xhelped me to reveal the following:

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

So it seems that the problem was coming from testngjar requiring JUnit v3.8.1. Even though Test Classpathwas set to depend on JUnit 4, it was too late.

因此,问题似乎来自testng需要 JUnit v3.8.1 的 jar。即使Test Classpath设置为依赖 JUnit 4,也为时已晚。

testngdependency was located in my POM:

testng依赖项位于我的 POM 中:

<dependency>
  <groupId>org.testng</groupId>
  <artifactId>testng</artifactId>
  <version>5.8</version>
  <scope>test</scope>
  <classifier>jdk15</classifier>
</dependency>

Immediately after I have commented it out, tests started to execute.

在我将其注释掉后,测试立即开始执行。

Lessons learned:

得到教训:

  • mvn dependency:treeis not always enough, mvn -Xis a friend.
  • surefire is not made for developer heaven (I have realized this while looking at project JIRA reports). This is especially true as there are no other alternatives if you use Maven.
  • mvn dependency:tree总是不够,mvn -X是朋友。
  • 万无一失不是为开发者天堂而生的(我在查看 JIRA 项目报告时意识到了这一点)。尤其如此,因为如果您使用 Maven,则没有其他选择。

Thanks everybody for your help. Unfortunately there is no way to split answer points between Pascal and Kaleb, but Kaleb's advice to use mvn -Xhelped me to get on the right track so correct answer points go to him.

谢谢大家的帮助。不幸的是,无法在 Pascal 和 Kaleb 之间分割答案点,但是 Kaleb 的建议mvn -X帮助我走上了正确的轨道,因此正确的答案点归于他。

回答by Andreas Dolk

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

您是否为正确的编译器级别配置了 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>

Otherwise maven will have trouble with annotations

否则 maven 会遇到注释问题

回答by Pascal Thivent

The verification that you've done are good, especially checking that you are using version 2.3+ of the surefire plugin (by default, you'll get version 2.4.3 with maven 2.1 super POMso this should be ok) and checking that you not pulling the junit-3.8.1.jardependency transitively.

您所做的验证很好,特别是检查您是否使用了surefire插件的2.3+版本(默认情况下,您将获得带有maven 2.1 super POM的2.4.3版本,所以这应该没问题)并检查您是否不junit-3.8.1.jar传递依赖。

Now, just to validated that this is not a "global problem" (I don't think so TBH), could you create a project from scratch, for example by running:

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

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

Then update the junit dependency:

然后更新junit依赖:

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

And configure the compiler level for 1.5+

并为 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>

Finally put your SimpleTest.javanext to AppTest.javaand run mvn test.

最后把你的SimpleTest.javanext to AppTest.javarun mvn test

If running mvn testworks fine for that project (and I'm expecting it to run without problem), could you please update your question with the POM configuration you're using (from the project having troubles)?

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

回答by Kaleb Pederson

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?

我不知道您所说的“无法执行”是什么意思,但是明确设置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 -Xflag provide any useful information?

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

回答by Randall Hauch

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"):

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

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:

但是请注意,Surefire 2.7 改变了它确定运行哪些单元测试类的方式。在 JUnit 4 中使用 Surefire 2.7(或更高版本)时的新行为是,任何没有 @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>
[...]

回答by David Carboni

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.

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

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).

我使用 POM 编辑器的 m2eclipse“Dependency Hierarchy”选项卡来查找依赖项并右键单击以生成排除项(请参阅下面的 XML)。

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:

为了完整性(以及对于那些不使用 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>

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

(就我而言,排除“powermock-module-testng”就足够了,但如果它来自其他地方,您可以直接排除 TestNG。)

回答by Gábor Lipták

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

另一个可能的原因可能是这个错误(以“不会修复”结束):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".

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

回答by MadhuGujjalapudi

One small change helped me Funnily!!!

一个小小的改变对我很有帮助!!!

I changed the class name from MyClassTest to TestMyClass, I got this idea after veryfing my parent POM.xml contains below line

我将类名从 MyClassTest 更改为 TestMyClass,在我的父 POM.xml 包含以下行之后我有了这个想法

<test.include.pattern> **/Test*.java <test.include.pattern/>

回答by James Selvakumar

For some poor soul out there who is wondering why Maven doesn't pick up JUnit tests.

对于那些想知道为什么 Maven 不接受 JUnit 测试的可怜人来说。

I have both JUnit and TestNG as dependecies. But I want failsafe to run my functional tests using TestNG and surefire to run my unit tests using JUnit.

我有 JUnit 和 TestNG 作为依赖项。但我希望故障安全使用 TestNG 运行我的功能测试,而 surefire 使用 JUnit 运行我的单元测试。

However, I found that surefire was attempting to run my unit tests using TestNG and found nothing to run. My JUnit tests were skipped.

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

Later I came across this Maven issueand configured surefire to run only "JUnit" tests like this:

后来我遇到了这个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>

Hope this helps someone.

希望这可以帮助某人。

回答by Ashutosh S

1.) Include following surefire plugin in pom.xml

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

<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/....

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

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

所以转到构建路径并在类路径中包含测试文件夹,如下所示:

enter image description hereenter image description here

在此处输入图片说明在此处输入图片说明

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

3.) 转到 --> 运行方式 --> 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