从Spock 1.2迁移到2.0-M2后,Maven surefire插件未运行测试 [英] Maven surefire plugin not running tests after migrating from Spock 1.2 to 2.0-M2

查看:136
本文介绍了从Spock 1.2迁移到2.0-M2后,Maven surefire插件未运行测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

工作设置-

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.com/ajaydivakaran/spock_spike

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

The intent of upgrading Spock was to make it compatible with jdk 11. The test class files are present in the target/test-classes folder but are not run.

推荐答案

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

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

Variant A: JUnit 4 + Spock 2 (Groovy 2.5)

In your Git repository I saw that your JUnit test imports org.junit.Test from JUnit 4 which you use as an undeclared transitive dependency used by Spock Core. For this you need the JUnit vintage engine or otherwise after you fixed running the Spock tests, the JUnit tests will no longer run.

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

If you name your unit tests according to Surefire convention, i.e. *Test, *Tests, *TestCase, Test* instead of the Spock convention *Spec, you also do not need to configure an <execution> section with extra includes. I just deleted it because your sample Spock test was named *Test already.

集成测试也是如此使用Maven Failsafe ,如果您以后要添加Failsafe,则命名约定为*IT*ITCaseIT*.

The same also applies to integration tests with Maven Failsafe where the naming convention is *IT, *ITCase, IT*, if you want to add Failsafe later.

Maven Build Helper插件也是多余的,因此我将其删除.

The Maven Build Helper plugin is also superfluous, so I removed it.

最后但并非最不重要的是,Spock 2依赖groovy作为常规导入,不再依赖groovy-all作为<type>pom</type>导入.

Last but not least, Spock 2 depends on groovy as a normal import, no longer on groovy-all as a <type>pom</type> import.

通过以下更改,您的测试可以很好地运行:

With the following changes your tests run nicely:

--- 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使用的JUnit 4.13和Spock使用的4.12.在JUnit 5中,老式引擎还使用了Spock Core之外的另一个平台引擎.

Dependency conflicts

On a side note, I found some dependency version conflicts in your project, e.g. Groovy versions 2.5.11 used by you vs. 2.5.8 used by Spock or JUnit 4.13 used by JUnit Jupiter vs. 4.12 used by Spock. Inside JUnit 5 the vintage engine also uses another platform engine than Spock Core.

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

In IntelliJ IDEA your dependency graph looks like this (red are conflicts):

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

With a dependency management section you can fix the conflicts and also simplify the dependency imports in your modules to not having to use the centrally managed version numbers or scopes anymore, as long as you do not wish to modify them for whatever reason.

您的变体B(JUnit 5)中的项目看起来像这样:

It would look like this for your project in variant 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>

现在,依赖关系图如下所示:

Now the dependency graph looks like this:

更新:不久之后,当尝试使用更多Spock时,例如使用CGLIB或ByteBuddy进行类模拟时,您会发现使用Groovy-Eclipse批处理不是一个好主意.带有Groovy 2.5的编译器3.0.正如我们在我对下一个问题的答案中所讨论的那样,您始终应该使用匹配的版本.因此,在这种情况下,您需要小心并保持Groovy编译器和Groovy版本同步:

Update: After a short while, when trying to use more of Spock, such as class mocking with CGLIB or ByteBuddy, you will notice that it is not a good idea to use Groovy-Eclipse batch compiler 3.0 with Groovy 2.5. You always should use a matching version, as we have just discussed in my answer to your next question. So you want to be careful and keep Groovy compiler and Groovy version in sync, in this case:

<groovy-eclipse-batch.version>2.5.11-01</groovy-eclipse-batch.version>

这篇关于从Spock 1.2迁移到2.0-M2后,Maven surefire插件未运行测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆