- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
工作设置 -
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 文件夹中,但未运行。
最佳答案
在您的 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>
我上面说的大部分内容仍然适用,只是您需要从 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 中,您的依赖关系图如下所示(红色为冲突):
通过依赖管理部分,您可以修复冲突并简化模块中的依赖导入,从而不必再使用集中管理的版本号或范围,只要您不希望出于任何原因修改它们。
对于变体 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 与 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/
在特征方法中,在 when: 中指定特征 Action 。块,其结果在后续 then: 中得到测试堵塞。通常需要准备,这在 given: 中完成条款(或 setup: 或夹具方法)。包含前提条件同样有
我尝试使用 Spy 测试但没有成功。下面的类是一个 Sut。 public class FileManager { public int removeFiles(String director
我希望能够在运行一些自动化测试时记录 spock 功能名称和子句标签。这将有助于在使用 headless 浏览器进行自动化时调试测试问题,特别是 phantomjs。原因是,phantomjs 的行为
如何以编程方式跳过 Spock 框架中的测试?我知道我可以 annotate a test with @Ignore 跳过它,或使用 @IgnoreIf跳过基于环境变量等的测试。但是有没有办法运行任意
下周我将做一个关于 Spock 的演讲,作为演讲的一部分,我需要做一个演示。我以前在一个项目中使用过 Spock,但大约一年左右没有使用它。 演示需要不仅仅是“hello world”类型的演示。我正
下周我将做一个关于 Spock 的演讲,作为演讲的一部分,我需要做一个演示。我以前在一个项目中使用过 Spock,但大约一年左右没有使用它。 演示需要不仅仅是“hello world”类型的演示。我正
为简单起见,我们来看一个非常简单的类: public class TestingClass { public void method1(){ System.out.printl
Spock 只允许从 where 块访问静态变量。 是否有任何解决方法可以在 where 块中使用哪些实例变量? 最佳答案 您可以使用 @Shared 注释实例变量,见 http://spockfra
我正在使用 Spock 框架进行测试,一切都很好,直到今天;我不知道发生了什么。 Intellij 说“配置 Groovy sdk”所以我下载了 groovy sdk 2.4.9 并配置了它,但是在我
我正在为 grails 2.1.1 应用程序的一组现有测试添加第一个 spock 集成测试。使用以下方法运行时,测试运行和测试通过: grails test-app integration:spock
我过去曾在其他项目中使用旧版本的 robolectric 使用 robospock 和 electricspock 对 robolectric 进行过 spock 测试。我的新项目使用 robolec
我正在使用 Maven Surefire 插件运行一组 Spock 测试作为集成测试用例。我知道我们可以使用 @Shared 关键字在单个文件中跨规范的固定装置共享资源。 但是,是否可以在不同的规范文
我正在与: Spock 核心 史波克报告 斯波克 Spring Spring MVC 测试 我有以下代码: def "findAll() Expected"(){ given: "The UR
我正在与: Spock 核心 史波克报告 斯波克 Spring Spring MVC 测试 我有以下代码: @FailsWith(java.lang.AssertionError.class) def
我正在为我的插件创建 Spock 测试 project-plugin我的主要项目名称是 main-project正在使用 project-plugin作为插件。因此,当我为我的插件创建 Spock 测
在JUnit 3中,我可以这样获得当前正在运行的测试的名称: public class MyTest extends TestCase { public void testSomething(
我有一些类似Java的东西: public interface EventBus{ void fireEvent(GwtEvent event); } public class SaveCom
在我的测试中,我有一些只需要在特定情况下运行的特征方法。我的代码看起来像这样: class MyTest extends GebReportingSpec{ def "Feature meth
在我的测试中,我有一些只需要在特定情况下运行的特征方法。我的代码看起来像这样: class MyTest extends GebReportingSpec{ def "Feature meth
我遇到的问题是当我尝试在 then 中验证时阻止已抛出异常,并且已进行模拟调用。 看看下面的设置: class B { def b(A a) { a.a() } } c
我是一名优秀的程序员,十分优秀!