Spock测试无法与由start.spring.io创建的新的spring-boot项目一起运行

问题描述

Spock测试可以在IDE中正常运行,但不能在Maven构建中执行。

要重新创建问题

  1. 转到https://start.spring.io/并创建一个新的Groovy / Maven项目,并保留所有其他认设置。
  2. 将spock依赖项添加到pom:
<dependency>
  <groupId>org.spockframework</groupId>
  <artifactId>spock-core</artifactId>
  <version>1.3-groovy-2.5</version>
  <scope>test</scope>
</dependency>
  1. 在现有测试旁边添加失败的spock测试,名为HelloSpec.groovy:
import spock.lang.Specification

class HelloSpec extends Specification {
  def "length of Spock's and his friends' names"() {
    expect:
    name.size() == length

    where:
    name     | length
    "Spock"  | 5
    "Kirk"   | 4
    "Scotty" | 6
    "Dave"   | 911
  }
}
  1. 将maven surefire插件添加到内部版本,以便它知道如何进行Spock测试。我知道这对于以Spec结尾的文件应该是自动的,但我还没有发现。我也了解该文件并非以.java结尾,实际上它是一个.groovy文件,只需随它一起即可。
<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-surefire-plugin</artifactId>
  <configuration>
    <includes>
      <include>**/*Spec.java</include>
    </includes>
  </configuration>
</plugin>
  1. 从命令行运行mvn clean install。您会发现自己在错误的greenbar上,并且spock测试无法运行。

解决方法

  1. 删除Spring添加到您的spring-boot-starter-tests依赖项中的junit-vintage-engine
<!--                <exclusion>-->
<!--                    <groupId>org.junit.vintage</groupId>-->
<!--                    <artifactId>junit-vintage-engine</artifactId>-->
<!--                </exclusion>-->
  1. 从命令行运行mvn clean install。 Spock测试被启动并运行,并且您的构建因测试失败而正确失败