我有一个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以来已经有一段时间了,所以我不是百分之百地确定我的库是正确的,但它确实试图运行,只是看起来不能加载上下文。
发布于 2021-11-19 13:04:00
您需要在项目中包含spock-spring。
在您的build.gradle中,用spock-spring替换其中的一个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'https://stackoverflow.com/questions/70029098
复制相似问题