【问题标题】:Spock test case doesn't work without JUnit's @Test annotation如果没有 JUnit 的 @Test 注解,Spock 测试用例将无法工作
【发布时间】:2020-07-08 10:24:55
【问题描述】:

我已经在 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 注释,但我不希望这样做。 另外 - 当我这样做时,测试仅在我运行整个班级时才有效。

我该如何解决这个错误?

【问题讨论】:

    标签: java spring-boot spock


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-09-08
      • 1970-01-01
      • 2020-01-12
      • 2013-01-21
      • 1970-01-01
      • 1970-01-01
      • 2019-05-30
      • 1970-01-01
      相关资源
      最近更新 更多