gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 00:00:34 25 4
gpt4 key购买 nike

工作设置 -

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 文件夹中,但未运行。

最佳答案

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

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

如果您根据 Surefire convention 命名您的单元测试,即 *Test , *Tests , *TestCase , Test*而不是 Spock 约定 *Spec , 你也不需要配置 <execution>包含额外内容的部分。我刚刚删除了它,因为您的样本 Spock 测试被命名为 *Test已经。

这同样适用于integration tests with 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 使用的 JUnit 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-Eclipse 批处理并不是一个好主意编译器 3.0 与 Groovy 2.5。正如我们刚刚在 my answer to your next question 中讨论的那样,您始终应该使用匹配的版本。 .所以你要小心并保持 Groovy 编译器和 Groovy 版本同步,在这种情况下:

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

关于从 Spock 1.2 迁移到 2.0-M2 后,Maven surefire 插件未运行测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61853267/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com