问题
Working setup -
Spock older version - 1.2-groovy-2.4
jdk version - 8
Maven surefire plugin version - 2.22.0
Maven version - 3.5.0
Migrated setup -
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
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.
回答1:
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.
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.
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.
The Maven Build Helper plugin is also superfluous, so I removed it.
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>
Variant B: JUnit 5 + Spock 2 (Groovy 2.5)
Most of what I said above still applies, only you need to switch from the JUnit 5 Vintage engine to the normal JUnit 5 Jupiter engine. Then you need to adjust the import in your JUnit test to org.junit.jupiter.api.Test
.
The diff to your project looks like this:
--- 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>
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.
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.
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:
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>
来源:https://stackoverflow.com/questions/61853267/maven-surefire-plugin-not-running-tests-after-migrating-from-spock-1-2-to-2-0-m2