在单元测试时,如何使用 Spock 访问通过 属性 文件访问的全局定义的 属性
How can I access a globally defined property that is accessed through a property file using Spock when unit testing
我正在尝试对一种方法进行单元测试,该方法使用使用@Value 注释从 属性 文件派生的全局定义变量。如何在单元测试中的服务 class 中访问这个全局 属性 而不必将其传递给方法签名。
例如:
Spring 名为 'SampleService'
的启动服务
@Value("${sampleproperty.value}")
private String sampleProperty;
private sampleMethodName(){
//method which uses the global variable 'sampleProperty'
}
样本 Spock 测试
def "Unit Test"(){
given:
//ToDo
when:
sampleService.sampleMethodName() //How to access the 'sampleProperty'
then:
//ToDo
}
在 Spock 和 groovy 测试中,您可以在任何地方通过变量名访问私有变量
def "Unit Test"(){
given:
sampleService.sampleProperty = "value" //to set the value
//ToDo
when:
sampleService.sampleMethodName() //How to access the 'sampleProperty'
then:
//ToDo
sampleService.sampleProperty == "value" //to access the value
}
我正在尝试对一种方法进行单元测试,该方法使用使用@Value 注释从 属性 文件派生的全局定义变量。如何在单元测试中的服务 class 中访问这个全局 属性 而不必将其传递给方法签名。
例如: Spring 名为 'SampleService'
的启动服务@Value("${sampleproperty.value}")
private String sampleProperty;
private sampleMethodName(){
//method which uses the global variable 'sampleProperty'
}
样本 Spock 测试
def "Unit Test"(){
given:
//ToDo
when:
sampleService.sampleMethodName() //How to access the 'sampleProperty'
then:
//ToDo
}
在 Spock 和 groovy 测试中,您可以在任何地方通过变量名访问私有变量
def "Unit Test"(){
given:
sampleService.sampleProperty = "value" //to set the value
//ToDo
when:
sampleService.sampleMethodName() //How to access the 'sampleProperty'
then:
//ToDo
sampleService.sampleProperty == "value" //to access the value
}