从 Spock 1.2 迁移到 2.0-M2 后,Maven surefire 插件没有运行测试

Aja*_*jay 2 maven spock maven-surefire-plugin java-11

工作设置 -

Spock older version - 1.2-groovy-2.4
jdk version - 8
Maven surefire plugin version - 2.22.0
Maven version - 3.5.0
Run Code Online (Sandbox Code Playgroud)

迁移设置 -

Spock version - 2.0-M2-groovy-2.5
jdk version - 11
Maven surefire plugin version - 3.0.0-M4
Maven version - 3.6.3
Run Code Online (Sandbox Code Playgroud)

MCVE - https://github.com/ajaydivakaran/spock_spike

升级 Spock 的目的是使其与 jdk 11 兼容。测试类文件存在于 target/test-classes 文件夹中,但不会运行。

kri*_*aex 5

变体 A:JUnit 4 + Spock 2(Groovy 2.5)

在您的 Git 存储库中,我看到您的 JUnit 测试org.junit.Test从 JUnit 4导入,您将其用作 Spock Core 使用的未声明的传递依赖项。为此,您需要 JUnit 老式引擎,否则在您修复运行 Spock 测试之后,JUnit 测试将不再运行。

如果您根据Surefire 约定命名您的单元测试,即*Test, *Tests, *TestCaseTest*而不是 Spock 约定*Spec,您也不需要配置<execution>带有额外包含的部分。我刚刚删除了它,因为您的示例 Spock 测试已经命名*Test

这同样适用于使用 Maven Failsafe 的集成测试,其中命名约定是*IT, *ITCase, IT*,如果您想稍后添加 Failsafe。

Maven Build Helper插件也是多余的,所以我把它去掉了。

最后但并非最不重要的,斯波克2取决于groovy作为一个正常的进口,不再groovy-all作为<type>pom</type>进口。

通过以下更改,您的测试运行良好:

--- pom.xml (revision 35c8e179569a7b45d48729d6cecf8170d02c8ed2)
+++ pom.xml (date 1589849467265)
@@ -25,6 +25,8 @@
         <groovy.version>2.5.11</groovy.version>
         <spock.version>2.0-M2-groovy-2.5</spock.version>

+        <junit.version>5.6.2</junit.version>
+
     </properties>

     <build>
@@ -64,45 +66,8 @@
                 <groupId>org.apache.maven.plugins</groupId>
                 <artifactId>maven-surefire-plugin</artifactId>
                 <version>${maven-surefire-plugin.version}</version>
-                <executions>
-                    <execution>
-                        <id>default-test</id>
-                        <phase>test</phase>
-                        <goals>
-                            <goal>test</goal>
-                        </goals>
-                        <configuration>
-                            <includes>
-                                <include>**/*Test.class</include>
-                                <include>**/*Spec.class</include>
-                                <include>**/*Should.class</include>
-                            </includes>
-                        </configuration>
-                    </execution>
-                </executions>
-            </plugin>
-
-            <plugin>
-                <groupId>org.codehaus.mojo</groupId>
-                <artifactId>build-helper-maven-plugin</artifactId>
-                <version>${build-helper-maven-plugin.version}</version>
-                <executions>
-                    <execution>
-                        <id>add-test-source</id>
-                        <phase>generate-test-sources</phase>
-                        <goals>
-                            <goal>add-test-source</goal>
-                        </goals>
-                        <configuration>
-                            <sources>
-                                <source>src/test/groovy</source>
-                            </sources>
-                        </configuration>
-                    </execution>
-                </executions>
             </plugin>

-
         </plugins>
     </build>

@@ -110,13 +75,18 @@

         <dependency>
             <groupId>org.codehaus.groovy</groupId>
-            <artifactId>groovy-all</artifactId>
+            <artifactId>groovy</artifactId>
             <version>${groovy.version}</version>
-            <type>pom</type>
+            <scope>test</scope>
+        </dependency>
+
+        <dependency>
+            <groupId>org.junit.vintage</groupId>
+            <artifactId>junit-vintage-engine</artifactId>
+            <version>${junit.version}</version>
             <scope>test</scope>
         </dependency>

-
         <dependency>
             <groupId>org.spockframework</groupId>
             <artifactId>spock-core</artifactId>
Run Code Online (Sandbox Code Playgroud)

变体 B:JUnit 5 + Spock 2(Groovy 2.5)

我上面说的大部分内容仍然适用,只是您需要从 JUnit 5 Vintage 引擎切换到普通的 JUnit 5 Jupiter 引擎。然后您需要将 JUnit 测试中的导入调整为org.junit.jupiter.api.Test.

与您的项目的差异如下所示:

--- src/test/java/me/spike/SubtractionTest.java (revision 35c8e179569a7b45d48729d6cecf8170d02c8ed2)
+++ src/test/java/me/spike/SubtractionTest.java (date 1589850279261)
@@ -1,6 +1,6 @@
 package me.spike;

-import org.junit.Test;
+import org.junit.jupiter.api.Test;

 import static org.junit.Assert.assertEquals;

--- pom.xml (revision 35c8e179569a7b45d48729d6cecf8170d02c8ed2)
+++ pom.xml (date 1589850279254)
@@ -25,6 +25,8 @@
         <groovy.version>2.5.11</groovy.version>
         <spock.version>2.0-M2-groovy-2.5</spock.version>

+        <junit.version>5.6.2</junit.version>
+
     </properties>

     <build>
@@ -64,45 +66,8 @@
                 <groupId>org.apache.maven.plugins</groupId>
                 <artifactId>maven-surefire-plugin</artifactId>
                 <version>${maven-surefire-plugin.version}</version>
-                <executions>
-                    <execution>
-                        <id>default-test</id>
-                        <phase>test</phase>
-                        <goals>
-                            <goal>test</goal>
-                        </goals>
-                        <configuration>
-                            <includes>
-                                <include>**/*Test.class</include>
-                                <include>**/*Spec.class</include>
-                                <include>**/*Should.class</include>
-                            </includes>
-                        </configuration>
-                    </execution>
-                </executions>
-            </plugin>
-
-            <plugin>
-                <groupId>org.codehaus.mojo</groupId>
-                <artifactId>build-helper-maven-plugin</artifactId>
-                <version>${build-helper-maven-plugin.version}</version>
-                <executions>
-                    <execution>
-                        <id>add-test-source</id>
-                        <phase>generate-test-sources</phase>
-                        <goals>
-                            <goal>add-test-source</goal>
-                        </goals>
-                        <configuration>
-                            <sources>
-                                <source>src/test/groovy</source>
-                            </sources>
-                        </configuration>
-                    </execution>
-                </executions>
             </plugin>

-
         </plugins>
     </build>

@@ -110,13 +75,24 @@

         <dependency>
             <groupId>org.codehaus.groovy</groupId>
-            <artifactId>groovy-all</artifactId>
+            <artifactId>groovy</artifactId>
             <version>${groovy.version}</version>
-            <type>pom</type>
+            <scope>test</scope>
+        </dependency>
+
+        <dependency>
+            <groupId>org.junit.jupiter</groupId>
+            <artifactId>junit-jupiter-api</artifactId>
+            <version>${junit.version}</version>
+            <scope>test</scope>
+        </dependency>
+        <dependency>
+            <groupId>org.junit.jupiter</groupId>
+            <artifactId>junit-jupiter-engine</artifactId>
+            <version>${junit.version}</version>
             <scope>test</scope>
         </dependency>

-
         <dependency>
             <groupId>org.spockframework</groupId>
             <artifactId>spock-core</artifactId>
Run Code Online (Sandbox Code Playgroud)

依赖冲突

附带说明一下,我在您的项目中发现了一些依赖版本冲突,例如您使用的 Groovy 版本 2.5.11 与 Spock 使用的 2.5.8 或 JUnit Jupiter 使用的 JUnit 4.13 与 Spock 使用的 4.12。在 JUnit 5 中,老式引擎还使用了除 Spock Core 之外的另一个平台引擎。

在 IntelliJ IDEA 中,您的依赖关系图如下所示(红色是冲突):

清理前的依赖图

通过依赖管理部分,您可以修复冲突并简化模块中的依赖导入,从而不必再使用集中管理的版本号或范围,只要您不希望出于任何原因修改它们。

对于变体 B (JUnit 5) 中的项目,它看起来像这样:

--- pom.xml (revision 35c8e179569a7b45d48729d6cecf8170d02c8ed2)
+++ pom.xml (date 1589849467265)
@@ -25,6 +25,8 @@
         <groovy.version>2.5.11</groovy.version>
         <spock.version>2.0-M2-groovy-2.5</spock.version>

+        <junit.version>5.6.2</junit.version>
+
     </properties>

     <build>
@@ -64,45 +66,8 @@
                 <groupId>org.apache.maven.plugins</groupId>
                 <artifactId>maven-surefire-plugin</artifactId>
                 <version>${maven-surefire-plugin.version}</version>
-                <executions>
-                    <execution>
-                        <id>default-test</id>
-                        <phase>test</phase>
-                        <goals>
-                            <goal>test</goal>
-                        </goals>
-                        <configuration>
-                            <includes>
-                                <include>**/*Test.class</include>
-                                <include>**/*Spec.class</include>
-                                <include>**/*Should.class</include>
-                            </includes>
-                        </configuration>
-                    </execution>
-                </executions>
-            </plugin>
-
-            <plugin>
-                <groupId>org.codehaus.mojo</groupId>
-                <artifactId>build-helper-maven-plugin</artifactId>
-                <version>${build-helper-maven-plugin.version}</version>
-                <executions>
-                    <execution>
-                        <id>add-test-source</id>
-                        <phase>generate-test-sources</phase>
-                        <goals>
-                            <goal>add-test-source</goal>
-                        </goals>
-                        <configuration>
-                            <sources>
-                                <source>src/test/groovy</source>
-                            </sources>
-                        </configuration>
-                    </execution>
-                </executions>
             </plugin>

-
         </plugins>
     </build>

@@ -110,13 +75,18 @@

         <dependency>
             <groupId>org.codehaus.groovy</groupId>
-            <artifactId>groovy-all</artifactId>
+            <artifactId>groovy</artifactId>
             <version>${groovy.version}</version>
-            <type>pom</type>
+            <scope>test</scope>
+        </dependency>
+
+        <dependency>
+            <groupId>org.junit.vintage</groupId>
+            <artifactId>junit-vintage-engine</artifactId>
+            <version>${junit.version}</version>
             <scope>test</scope>
         </dependency>

-
         <dependency>
             <groupId>org.spockframework</groupId>
             <artifactId>spock-core</artifactId>
Run Code Online (Sandbox Code Playgroud)

现在依赖图看起来像这样:

清理后的依赖图


更新:不久之后,当尝试使用更多 Spock 时,例如使用 CGLIB 或 ByteBuddy 进行类模拟时,您会注意到将 Groovy-Eclipse 批处理编译器 3.0 与 Groovy 2.5 一起使用并不是一个好主意。您应该始终使用匹配的版本,正如我们刚刚在我对您的下一个问题的回答中所讨论的。所以你要小心并保持 Groovy 编译器和 Groovy 版本同步,在这种情况下:

--- src/test/java/me/spike/SubtractionTest.java (revision 35c8e179569a7b45d48729d6cecf8170d02c8ed2)
+++ src/test/java/me/spike/SubtractionTest.java (date 1589850279261)
@@ -1,6 +1,6 @@
 package me.spike;

-import org.junit.Test;
+import org.junit.jupiter.api.Test;

 import static org.junit.Assert.assertEquals;

--- pom.xml (revision 35c8e179569a7b45d48729d6cecf8170d02c8ed2)
+++ pom.xml (date 1589850279254)
@@ -25,6 +25,8 @@
         <groovy.version>2.5.11</groovy.version>
         <spock.version>2.0-M2-groovy-2.5</spock.version>

+        <junit.version>5.6.2</junit.version>
+
     </properties>

     <build>
@@ -64,45 +66,8 @@
                 <groupId>org.apache.maven.plugins</groupId>
                 <artifactId>maven-surefire-plugin</artifactId>
                 <version>${maven-surefire-plugin.version}</version>
-                <executions>
-                    <execution>
-                        <id>default-test</id>
-                        <phase>test</phase>
-                        <goals>
-                            <goal>test</goal>
-                        </goals>
-                        <configuration>
-                            <includes>
-                                <include>**/*Test.class</include>
-                                <include>**/*Spec.class</include>
-                                <include>**/*Should.class</include>
-                            </includes>
-                        </configuration>
-                    </execution>
-                </executions>
-            </plugin>
-
-            <plugin>
-                <groupId>org.codehaus.mojo</groupId>
-                <artifactId>build-helper-maven-plugin</artifactId>
-                <version>${build-helper-maven-plugin.version}</version>
-                <executions>
-                    <execution>
-                        <id>add-test-source</id>
-                        <phase>generate-test-sources</phase>
-                        <goals>
-                            <goal>add-test-source</goal>
-                        </goals>
-                        <configuration>
-                            <sources>
-                                <source>src/test/groovy</source>
-                            </sources>
-                        </configuration>
-                    </execution>
-                </executions>
             </plugin>

-
         </plugins>
     </build>

@@ -110,13 +75,24 @@

         <dependency>
             <groupId>org.codehaus.groovy</groupId>
-            <artifactId>groovy-all</artifactId>
+            <artifactId>groovy</artifactId>
             <version>${groovy.version}</version>
-            <type>pom</type>
+            <scope>test</scope>
+        </dependency>
+
+        <dependency>
+            <groupId>org.junit.jupiter</groupId>
+            <artifactId>junit-jupiter-api</artifactId>
+            <version>${junit.version}</version>
+            <scope>test</scope>
+        </dependency>
+        <dependency>
+            <groupId>org.junit.jupiter</groupId>
+            <artifactId>junit-jupiter-engine</artifactId>
+            <version>${junit.version}</version>
             <scope>test</scope>
         </dependency>

-
         <dependency>
             <groupId>org.spockframework</groupId>
             <artifactId>spock-core</artifactId>
Run Code Online (Sandbox Code Playgroud)