从Spock 1.2迁移到2.0-M2后,Maven surefire插件无法运行测试。

问题描述 投票:0回答:1

工作设置 -

Spock older version - 1.2-groovy-2.4
jdk version - 8
Maven surefire plugin version - 2.22.0
Maven version - 3.5.0

迁移后的设置 -

Spock version - 2.0-M2-groovy-2.5
jdk version - 11
Maven surefire plugin version - 3.0.0-M4
Maven version - 3.6.3

MCVE - https:/github.comajaydivakaranspock_spike。

升级Spock的目的是为了使其兼容jdk 11.测试类文件存在于targettest-classes文件夹中,但没有运行。

maven spock java-11 maven-surefire-plugin
1个回答
2
投票

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

在你的Git仓库中,我看到你的JUnit测试导入了 org.junit.Test 从JUnit 4中,你把它作为Spock Core使用的未声明的转义依赖。为此,你需要JUnit vintage引擎,否则在你固定运行Spock测试后,JUnit测试将不再运行。

如果你把你的单元测试命名为 必杀技公约*Test, *Tests, *TestCase, Test* 非斯波克公约 *Spec,你也不需要配置一个 <execution> 额外包含的部分。我只是把它删掉了,因为你的斯波克测试样本被命名为 *Test 已经。

同样的情况也适用于 使用Maven Failsafe进行集成测试 其中的命名习惯是 *IT, *ITCase, IT*,如果你以后想添加Failsafe。

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

最后但并非最不重要的一点是,Spock 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>

变体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>

依赖性冲突

另外,我发现你的项目中存在一些依赖版本的冲突,比如你使用的Groovy版本2.5.11与Spock使用的2.5.8,或者JUnit Jupiter使用的4.13与Spock使用的4.12。在JUnit 5里面,复古引擎也使用了另一个平台引擎,而不是Spock Core。

在IntelliJ IDEA中,你的依赖关系图是这样的(红色是冲突)。

Dependency graph before clean-up

有了依赖关系管理部分,你可以修复冲突,也可以简化模块中的依赖关系导入,不必再使用集中管理的版本号或作用域,只要你不希望因为任何原因修改它们。

你的项目在变体B(JUnit 5)中会是这样的。

    <dependencyManagement>
        <dependencies>

            <dependency>
                <groupId>org.codehaus.groovy</groupId>
                <artifactId>groovy</artifactId>
                <version>${groovy.version}</version>
                <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>
                <version>${spock.version}</version>
                <scope>test</scope>
            </dependency>

            <dependency>
                <groupId>org.junit.platform</groupId>
                <artifactId>junit-platform-engine</artifactId>
                <version>1.6.2</version>
                <scope>test</scope>
            </dependency>

        </dependencies>
    </dependencyManagement>

    <dependencies>

        <dependency>
            <groupId>org.codehaus.groovy</groupId>
            <artifactId>groovy</artifactId>
        </dependency>

        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-api</artifactId>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-engine</artifactId>
        </dependency>

        <dependency>
            <groupId>org.spockframework</groupId>
            <artifactId>spock-core</artifactId>
        </dependency>

    </dependencies>

现在依赖关系图看起来是这样的。

Dependency graph after clean-up


更新。 过了不久,当你尝试使用更多的Spock时,比如使用CGLIB或ByteBuddy进行类嘲讽,你会注意到,在Groovy 2.5中使用Groovy-Eclipse批量编译器3.0不是一个好主意。你总是应该使用匹配的版本,就像我们刚才在 我对你下一个问题的回答是. 所以你要小心,保持Groovy编译器和Groovy版本同步,在这种情况下。

<groovy-eclipse-batch.version>2.5.11-01</groovy-eclipse-batch.version>
© www.soinside.com 2019 - 2024. All rights reserved.