【问题标题】:How to access application.yml file from the spock test framework如何从 spock 测试框架访问 application.yml 文件
【发布时间】:2019-09-03 14:28:52
【问题描述】:

我为与下游服务交互的 Spring Boot 应用程序配置了集成测试。我想从 spring 应用程序中使用的 application.yml 配置 spock 测试中使用的一些变量。这在Spockgroovy 中是否可行。

我尝试使用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


【解决方案1】:

您的规范看起来不完整,单个 then: 块不是对 Spock 的有效测试。 如果你想要类似的东西,你需要expect:

这看起来像一个 Spring Boot 项目,如果是这样,你应该使用 @SpringBootTest 而不是 @ContextConfiguration。还要确保您有 spock-spring 依赖项。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-10-24
    • 2016-09-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-21
    相关资源
    最近更新 更多