Maven/Surefire无法在同一项目中执行Spock和JUnit [英] Maven/Surefire can't execute Spock and JUnit in the same project
问题描述
- java
$ java -version
java version "1.8.0_241"
Java(TM) SE Runtime Environment (build 1.8.0_241-b07)
Java HotSpot(TM) 64-Bit Server VM (build 25.241-b07, mixed mode)
- 行家
$ mvn -version
Apache Maven 3.6.3 (cecedd343002696d0abb50b32b541b8a6ba2883f)
Maven home: C:\Program Files\JetBrains\IntelliJ IDEA 2019.2.3\plugins\maven\lib\maven3
Java version: 1.8.0_231, vendor: Oracle Corporation, runtime: C:\Program Files\Java\jdk1.8.0_231\jre
Default locale: zh_CN, platform encoding: GBK
OS name: "windows 10", version: "10.0", arch: "amd64", family: "windows"
- pom.xml -从 spockframework/spock-example
- pom.xml -- Forked from spockframework/spock-example
- MVN干净测试
- MVN干净测试-Dtest = HelloJUnitTest
- JUnit Test已在 target/test-classs * 中生成
- JUnit Test had generate in target/test-classes*
- MVN干净测试-Dtest = HelloSpocSpec
- mvn -X包括测试步骤
我分叉了 spockframework/spock-example 添加Java测试目录和HelloJUnitTest.java => ; sunzy/spock-exmaple
I forked spockframework/spock-example add java test directory and HelloJUnitTest.java => sunzy/spock-exmaple
可以执行Spock测试,但不能执行JUnit测试
仅spock测试!
推荐答案
好的,我看过您的项目.正如您所说,这只是Spock示例项目,已升级为运行Spock 2测试. BTW,它仍然应该进一步升级,因为在当前配置中,编译不适用于当前的Java版本,但这不在本文的讨论范围内,我只是在提及它,因为我遇到了问题然后降级为Java 8为了快速重现您的实际问题.即使默认程序包总是很丑,就像Spock测试一样,JUnit测试的程序包名称也不是问题.
Okay, I have taken a look at your project. As you said, it is just the Spock sample project, upgraded to run Spock 2 tests. BTW, it still should be upgraded further, because in the current configuration compilation does not work with current Java versions, but that is off topic here, I am just mentioning it because I ran into a problem and then downgraded to Java 8 in order quickly reproduce your actual problem. The JUnit test's package name is not the problem either, even though the default package is always ugly, just like for the Spock tests.
Spock 1.x基于JUnit 4,但是Spock 2.x基于JUnit 5平台.这也是Surefire在分析项目依赖项时自动找到的一个.如果希望Surefire并行运行多个引擎,则需要将相应的提供程序配置为插件依赖项,如
Spock 1.x is based on JUnit 4, but Spock 2.x is based on JUnit 5 platform. This is also the one automatically found by Surefire when analysing the project dependencies. If you want Surefire to run multiple engines in parallel, you need to configure the corresponding providers as plugin dependencies, as mentioned here.
在您的情况下,只需将其添加到POM:
In your case, just add this to the POM:
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M4</version>
<configuration>
<useFile>false</useFile>
<includes>
<include>**/*Test.java</include>
<include>**/*Spec.java</include>
</includes>
</configuration>
<!-- Run Spock 2 and JUnit 4 tests in parallel -->
<dependencies>
<dependency>
<groupId>org.apache.maven.surefire</groupId>
<artifactId>surefire-junit47</artifactId>
<version>3.0.0-M4</version>
</dependency>
<dependency>
<groupId>org.apache.maven.surefire</groupId>
<artifactId>surefire-junit-platform</artifactId>
<version>3.0.0-M4</version>
</dependency>
</dependencies>
</plugin>
将对JUnit 4.12或4.13的测试范围的依赖项显式添加到您的POM也很有意义,因为JUnit 4.12只是当前POM中的传递性依赖项.但是更高版本的Spock 2可能会删除该依赖关系,因为它并不是真正需要的.我认为2.0-M3已经发生了这种情况.所以要小心.
It also makes sense to explicitly add a test-scoped dependency to JUnit 4.12 or 4.13 to your POM because JUnit 4.12 is only a transitive dependency in your current POM. But later Spock 2 versions might remove that dependency because it is not really needed. I think that already happened with 2.0-M3. So be careful.
更改之后,Maven说:
After this change, Maven says:
[INFO] --- maven-surefire-plugin:3.0.0-M4:test (default-test) @ spock-example ---
[INFO]
[INFO] -------------------------------------------------------
[INFO] T E S T S
[INFO] -------------------------------------------------------
[INFO] Running HelloJUnitTest
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.059 s - in HelloJUnitTest
[INFO]
[INFO] Results:
[INFO]
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
[INFO]
[INFO]
[INFO] -------------------------------------------------------
[INFO] T E S T S
[INFO] -------------------------------------------------------
[INFO] Running DatabaseDrivenSpec
[INFO] Tests run: 3, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.171 s - in DatabaseDrivenSpec
(...)
[INFO]
[INFO] Results:
[INFO]
[INFO] Tests run: 39, Failures: 0, Errors: 0, Skipped: 0
如果您决定从JUnit 4升级到5,则配置可能会更容易,因为Spock和JUnit都使用相同的提供程序.在这种情况下,请添加一个JUnit 5依赖项,以便您的测试可以导入相应的测试注释和断言方法.
If you decide to upgrade from JUnit 4 to 5, maybe configuration gets easier for you because then both Spock and JUnit use the same provider. In that case please add a JUnit 5 dependency so your tests can import the corresponding test annotations and assertion methods.
更新:@khmarbaise评论使用老式引擎并且我完全同意这是更好的解决方案之后,我想向您展示如何做到这一点,而不是向Surefire添加插件依赖项. (因此,如果要使用此解决方案,则可以删除它们):
Update: After @khmarbaise commented on using vintage engine and me fully agreeing that it is the better solution, I want to show you how to do that instead of adding plugin dependencies to Surefire. (So you can delete those if you want to use this solution):
<dependency> <!-- only required if you want to run JUnit 4 tests alongside Spock 2 -->
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
<version>5.5.2</version>
<scope>test</scope>
</dependency>
为什么是版本5.5.2而不是例如5.6.2?为了避免版本冲突和有关老式引擎未在Groovy目录中找到测试的后续警告.这仅仅是因为此示例项目中的POM仍使用Spock 2.0-M1 BOM.正如我所说,应该对其进行更新.但是使用此版本,它就可以工作,因为在此配置中,它依赖于与Spock相同的JUnit 5平台版本.
Why version 5.5.2 and not e.g. 5.6.2? In order to avoid version conflicts and subsequent warnings about vintage engine not finding tests in the Groovy directory. This is just because the POM in this sample project still uses a Spock 2.0-M1 BOM. As I said, it ought to be updated. But with this version it just works because it depends on the same JUnit 5 platform version as Spock in this configuration.
顺便说一句,现在Maven首先执行Spock测试,然后再执行JUnit 4测试,因此两者的日志输出都将相反.
BTW, now Maven executes the Spock tests first and then the JUnit 4 tests, so the log output for both would be in reverse order.
这篇关于Maven/Surefire无法在同一项目中执行Spock和JUnit的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!