当前位置: 首页 > 知识库问答 >
问题:

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

端木安国
2023-03-14

工作设置-

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

升级Spock的目的是使其与JDK11兼容。测试类文件位于target/test-classs文件夹中,但未运行。

共有1个答案

章睿
2023-03-14

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

如果您根据Surefire约定命名单元测试,即*test*tests*testcasetest*,而不是Spock约定*spec,您也不需要配置具有额外包含的 部分。我刚刚删除了它,因为您的示例Spock测试已经命名为*test

这同样适用于Maven Failsafe的集成测试,其中命名约定为*it*itcaseit*,如果您以后要添加Failsafe的话。

Maven Build Helper插件也是多余的,所以我删除了它。

最后但并非最不重要的一点是,Spock 2依赖于groovy作为正常导入,而不再依赖于groovy-all作为 POM 导入。

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

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

我上面说的大部分仍然适用,只是你需要从JUnit 5复古引擎切换到普通的JUnit 5木星引擎。然后您需要将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>

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

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

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>

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

更新:过了一会儿,当尝试使用更多的Spock时,比如使用CGLIB或ByteBuddy进行类模拟时,您会注意到将Groovy-Eclipse批处理编译器3.0与Groovy2.5一起使用并不是一个好主意。你总是应该使用一个匹配的版本,正如我们刚才在我对你下一个问题的回答中所讨论的。所以您希望小心并保持Groovy编译器和Groovy版本同步,在本例中:

<groovy-eclipse-batch.version>2.5.11-01</groovy-eclipse-batch.version>
 类似资料:
  • 我已经开始与演员一起工作,并遵循入门指南中提到的一个简单的例子。 名称:=“Pracakka” scalaVersion:=“2.9.2” 解析器+=“TypeSafe Repository”(位于“http://repo.typesafe.com/typesafe/releases/”)

  • 我正在做一个项目,客户现在从Java6转到Java8。我们在项目中下载一个jnlp文件来运行一个java应用程序。在Java6中,我们没有问题,但是现在迁移到Java8后,我们面临着安全问题,因为我们不能再使用自签名jar了。客户给我们提供了一份证书。我的问题是如何在Java8中运行那个jnlp文件?我尝试在jnlp文件中使用的jar上签名。我使用Java提供的cacerts密钥库。我运行以下命令

  • 问题内容: 我正在使用JSF 1.2编写的大型应用程序。JSF 1.2大约已有6年历史了。我需要升级到JSF 2.0。这将有多痛苦?我注意到自定义标签中的某些属性已更改等。 问题答案: Painfulness 将JSF 1.2升级到2.0的痛苦程度取决于你当前正在使用以及要使用的视图技术。 从JSP 2.x到JSP 2.x =几乎无需付出任何努力。 从Facelets 1.x到Facelets 2

  • 迁移包括以下部分: MIP 核心库迁移 将站点引用的 mip.js 和 mip.css 版本由 1.0 修改为 2.0。MIP 核心库 2.0 版本完全兼容 1.0 版本,开发者可放心升级。具体做法如下: mip.js 文件迁移 将站点中的所有如下引用: <script src="https://c.mipcdn.com/static/v1/mip.js"></script> 改为: <scrip

  • 下面是我的pom.xml、testng.xml文件和TestRunners。 下面是我的pom.xml文件 [Utils][ERROR][ERROR]java.lang.NullPointerException在Cucumber.api.testng.AbstractTestngCucumberTests.Scenaries(AbstractTestngCucumberTests.java:31)

  • 问题内容: 我在夹层中使用Django1.7。我创建了简单的配置文件(根据Mezzanine文档),存储在单独的应用程序“配置文件”中: 创建迁移会返回: 当我运行“迁移配置文件”时: 问题是,当我尝试打开与mezzanine.accounts相关的任何页面(例如更新帐户)时,它崩溃并显示: 我做错了什么? 问题答案: 在MySQL数据库中,从表中删除行。 删除迁移文件夹中的所有迁移文件。 重试并