【问题标题】:spring boot & Spock弹簧靴和斯波克
【发布时间】:2021-12-29 21:20:08
【问题描述】:

我有一个 springBootVersion = '2.3.2.RELEASE' 应用程序,我想将 Spock 用于我的单元和集成测试。我在 Gradle 中为 Spock 准备了这个:

  testImplementation('org.springframework.boot:spring-boot-starter-test') {
        exclude group: 'junit', module: 'junit' //by both name and group
    }

  testImplementation group: 'org.spockframework', name: 'spock-core', version: '1.2-groovy-2.4'
 testImplementation group: 'org.spockframework', name: 'spock-core', version: '1.2-groovy-2.4'
 implementation group: 'org.codehaus.gmavenplus', name: 'gmavenplus-plugin', version: '1.6.3'

我的第一个测试非常简单:

@SpringBootTest(classes = PrototypeApplication.class)
class PrototypeApplicationSpec extends Specification {

    @Autowired
    ApplicationContext context

    def "test context loads"() {
        expect:
        context != null
    }
}

但是当我运行它时,ApplicationContext 为空:

> Task :compileJava UP-TO-DATE
> Task :compileGroovy NO-SOURCE
> Task :processResources UP-TO-DATE
> Task :classes UP-TO-DATE
> Task :compileTestJava UP-TO-DATE
> Task :compileTestGroovy
> Task :processTestResources UP-TO-DATE
> Task :testClasses
> Task :test FAILED

Condition not satisfied:

> context != null |       | null    false
> 
> Condition not satisfied:
> 
> context != null |       | null    false

自从我使用 Spock 已经有一段时间了,所以我不是 100% 认为我的库是正确的,但它确实尝试运行,只是似乎无法加载上下文。

【问题讨论】:

  • 请升级 Spock 版本 1.2-groovy-2.4 确实很旧,但 Spring 版本也是如此。
  • 是的,我需要,但这可能还需要 Gradle 和其他更改。

标签: spock spring-test


【解决方案1】:

您需要在项目中包含spock-spring

spock-spring 替换build.gradle 中的spock-core 重复项:

testImplementation group: 'org.spockframework', name: 'spock-core', version: '1.2-groovy-2.4'
testImplementation group: 'org.spockframework', name: 'spock-spring', version: '1.2-groovy-2.4'

【讨论】:

  • 谢谢,我确实没看到那个错字。
猜你喜欢
  • 2017-08-18
  • 1970-01-01
  • 2017-12-22
  • 2021-05-28
  • 2018-02-14
  • 2019-05-03
  • 2021-04-20
  • 1970-01-01
  • 2023-01-07
相关资源
最近更新 更多