首页 > 解决方案 > 如果没有 JUnit 的 @Test 注解,Spock 测试用例将无法工作

问题描述

我已经在 IntelliJ 中启动了新的 java 11、spring boot 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 注释,但我不希望这样做。另外-当我这样做时,测试仅在我运行整个班级时才有效。

如何解决错误?

标签: javaspring-bootspock

解决方案


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


推荐阅读