如何从 spock 测试框架访问 application.yml 文件
How to access application.yml file from the spock test framework
我为与下游服务交互的 spring 引导应用程序配置了集成测试。我想从 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 引导项目,如果是这样,您应该使用 @SpringBootTest
而不是 @ContextConfiguration
。还要确保您具有 spock-spring
依赖项。
我为与下游服务交互的 spring 引导应用程序配置了集成测试。我想从 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 引导项目,如果是这样,您应该使用 @SpringBootTest
而不是 @ContextConfiguration
。还要确保您具有 spock-spring
依赖项。