如果没有 JUnit 的 @Test 注释,Spock 测试用例将无法工作

Spock test case doesn't work without JUnit's @Test annotation

我已经在 IntelliJ 中启动了新的 java11,springboot 2.3.1 项目。 我想将 spock 添加到依赖项中,但是在尝试 运行 示例测试用例时遇到问题。

org.junit.runners.model.InvalidTestClassError: Invalid test class 'com.example.SpockSpec':
1. No runnable methods

这是我的依赖项列表:

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter'
    testImplementation('org.springframework.boot:spring-boot-starter-test') {
        exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
    }
    testImplementation "org.codehaus.groovy:groovy-all:$groovyVersion"
    testImplementation 'org.spockframework:spock-core:2.0-M2-groovy-3.0'
}

(groovy 版本为 3.0.0)

这是规范:

class SpockSpec extends Specification {

    def "result should be true"(){
        given:
        boolean a = false;
        boolean b = true;

        when:
        boolean result = a || b;

        then:
        result == true;
    }
}

解决方法是在该方法之前添加 JUnit 的 @Test 注释,但我不想这样做。 另外 - 当我这样做时,测试仅在我 运行 整个 class.

时有效

如何解决错误?

Spock 1 基于 JUnit 4,但 Spock 2 运行在 JUnit 5 平台上,实现了自己的引擎(不是 JUnit Jupiter)。所以也许您想先阅读一些文档,然后再升级到 JUnit 5(不是 vintage)。在“入门”一章的手册顶部有一个子章 "Spock example project"