Java Surefire不会接受Junit 4测试
由于某些原因,我无法让Maven 2 Surefire插件执行JUnit 4测试类Java Surefire不会接受Junit 4测试,java,maven-2,maven,surefire,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
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版本:ApacheMaven 2.2.1(r801777;2009-08-06 20:16:01+0100)
- 已验证的Surefire版本:遵循建议
- 已验证的Surefire版本:在my
中检查了Surefire JAR——它们都是版本2.4.2或2.4.3~/.m2/repository/org/apache/maven/Surefire
- 完成了一个
,以确保我只依赖junit版本4.7mvn依赖项:tree | grep junit
还有什么我遗漏的吗?您是否为正确的编译器级别配置了maven编译插件,如:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.5</source>
<target>1.5</target>
</configuration>
</plugin>
org.apache.maven.plugins
maven编译器插件
1.5
1.5
否则maven将在注释方面遇到问题,您所做的验证是好的,特别是检查您是否正在使用surefire插件的2.3+版本(默认情况下,您将使用maven 2.1获得2.4.3版本,所以这应该是可以的),并检查您是否可以传递性地拉取
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>
朱尼特
朱尼特
4.7
测试
并将编译器级别配置为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>
org.apache.maven.plugins
maven编译器插件
1.5
1.5
最后,将您的SimpleTest.java
放在AppTest.java
旁边,然后运行mvn测试
如果运行mvn测试
可以很好地运行该项目(我希望它可以毫无问题地运行),请用您正在使用的POM配置更新您的问题(来自有问题的项目)我不知道您所说的“无法执行”是什么意思但是显式设置maven-surefire插件使用的include是否有帮助
<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>
org.apache.maven.plugins
maven surefire插件
2.4.3
**/*Test.java
另外,运行带有-X
标志的maven是否提供了有用的信息?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
因此,问题似乎来自需要JUnit v3.8.1的testng
jar。尽管testclasspath
被设置为依赖于JUnit4,但为时已晚
testng
依赖项位于我的POM中:
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>5.8</version>
<scope>test</scope>
<classifier>jdk15</classifier>
</dependency>
org.testng
testng
5.8
测试
jdk15
在我注释掉它之后,测试立即开始执行
经验教训:
mvn依赖关系:树
并不总是足够的,mvn-X
是一个朋友
- surefire不是为开发者天堂(我在查看项目JIRA报告时意识到了这一点)。这一点尤其正确,因为如果使用Maven,就没有其他选择
谢谢大家的帮助。不幸的是,没有办法在Pascal和Kaleb之间划分答案,但是Kaleb关于使用mvn-X
的建议帮助我走上了正确的道路,所以正确的答案归他。Surefire插件根据类路径确定了应该使用哪个JUnit提供程序。如果类路径上有多个JUnit版本,您可以将类路径更正为类路径上只有一个JUnit版本(如上所述),也可以显式指定要使用的提供程序。例如,在(父)POM中指定以下内容将强制使用最新的提供程序(例如,“surefire-junit47”):
[…]
org.apache.maven.plugins
maven surefire插件
2.8
org.apache.maven.surefire
surefire-junit47
2.8
[...]
但是请注意,Surefire 2.7改变了确定运行哪些单元测试类的方式在JUnit 4中使用Surefire 2.7(或更高版本)时的新行为是,任何没有@test注释的测试都将自动跳过。如果您只有JUnit 4单元测试,这可能会很好,但是如果您有JUnit 3和JUnit 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>
[...]
[…]
org.apache.maven.plugins
maven surefire插件
2.8
org.apache.maven.surefire
surefire-junit4
2.8
[...]
为了谷歌的利益,当我遇到这个问题时,是因为我在TestNG中包含了一个PowerMock依赖项,导致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>
org.powermock
powermock mockito完全发布
1.4.9
满的
powermock模块testng
org.powermock
(在我的例子中,排除“powermock module testng”就足够了,但如果testng是从其他地方传入的,您可以直接排除它。)还有一个可能的原因可能是此错误(以“不会修复”结束):
简短总结:扩展TestCase(但不使用注释)的测试如果名称不以“Test”结尾,将不会被接受。一个小小的更改很有趣地帮助了我
我将类名从MyClassTest更改为TestMyClass,
我是在仔细检查了我的父母POM.xml包含的以下内容后得出这个想法的
<test.include.pattern> **/Test*.java <test.include.pattern/>
**/Test*.java
为了一些可怜的苏
<test.include.pattern> **/Test*.java <test.include.pattern/>
<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>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.20</version>
<configuration>
</configuration>
</plugin>
[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
<dependency>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
<scope>test</scope>
</dependency>