英文:
Maven/Surefire can't execute Spock and JUnit in the same project
问题
Env(环境)
- java(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)
- maven(Maven版本)
$ 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 -- Forked from spockframework/spock-example(pom.xml配置文件,从spockframework/spock-example分支派生)
Problem(问题)
我从spockframework/spock-example分支派生出来,添加了Java测试目录和HelloJUnitTest.java => sunzy/spock-exmaple
Spock测试可以执行,但JUnit测试不能执行
Snapshots(快照)
- mvn clean test
仅运行Spock测试!
- mvn clean test -Dtest=HelloJUnitTest
- JUnit测试已生成在target/test-classes目录下
- mvn clean test -Dtest=HelloSpocSpec
-
mvn -X include test-clasess
英文:
Env
- 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)
- maven
$ 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 -- Forked from spockframework/spock-example
Problem
I forked spockframework/spock-example add java test directory and HelloJUnitTest.java => sunzy/spock-exmaple
the Spock Tests can execute,but JUnit Tests can't
Snapshots
- mvn clean test
only spock tests!
- mvn clean test -Dtest=HelloJUnitTest
- JUnit Test had generate in target/test-classes*
- mvn clean test -Dtest=HelloSpocSpec
-
mvn -X include test-clasess
答案1
得分: 11
以下是您要翻译的内容:
"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 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.
In your case, just add this to the POM:
<!-- language: xml -->
<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>
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.
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
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.
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):
<!-- language: xml -->
<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>
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.
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.
英文:
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 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.
In your case, just add this to the POM:
<!-- language: xml -->
<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>
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.
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
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.
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):
<!-- language: xml -->
<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>
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.
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论