【发布时间】:2019-09-03 14:28:52
【问题描述】:
我为与下游服务交互的 Spring Boot 应用程序配置了集成测试。我想从 spring 应用程序中使用的 application.yml 配置 spock 测试中使用的一些变量。这在Spock 和groovy 中是否可行。
我尝试使用Spring 注释来捕获数据,就像它在Spring 应用程序中所做的那样。
我尝试过使用下面的代码,但它总是为空。
@Value('test.base.url')
def variableName
我还尝试使用@PropertySource('classpath:application.yml') 指向application.yml
package com.company.package1
import groovyx.net.http.RESTClient
import org.junit.experimental.categories.Category
import org.springframework.http.HttpStatus
import org.springframework.http.MediaType
import org.springframework.test.context.ContextConfiguration
import spock.lang.Shared
import spock.lang.Specification
import spock.lang.Unroll
@ContextConfiguration(classes = Application.class)
@Category(IntegrationTest.class)
class GetVariableTests extends Specification {
@Value('test.base.url')
def variableName
def "test variable name"() {
then:
variableName == "http://localhost:8020/"
}
}
application.yml
test:
base:
url:"http://localhost:8020/"
我希望通过这个测试。但是variableName 始终为空。
【问题讨论】:
-
您的规范看起来不完整,单个
then:块不是对 spock 的有效测试。如果你想要类似的东西,你需要expect:。这看起来像一个 Spring Boot 项目,如果是这样,你应该使用@SpringBootTest而不是@ContextConfiguration。还要确保你有spock-spring依赖项。 -
感谢您的回复。我能够使其与 @SpringBootTest 和 spock-spring 依赖项一起工作
标签: spock