【问题标题】:Removing the hardcoded urls from spock test case in post request在 post 请求中从 spock 测试用例中删除硬编码的 url
【发布时间】:2019-09-01 06:58:34
【问题描述】:

我已经编写了基本的 Spring Spock 测试并且我能够成功地做到这一点,但关键是现在我在我的帖子请求中调用的 URL 不是相对的,而是指向我的本地的

http://localhost:88/rty/user/user1/applications/ZZZ78R/users/409?username=ABCedit

所以如果这在测试环境中运行,localhost 将不可用,所以请告知我应该如何在下面的 Spock 测试用例中更正我的 URL

@SpringBootTest
class AuthenticationFactorControllerSpec extends Specification {
    def "Should get status 201 when server is up and post request is made"() {
        when:
        HttpEntity<String> httpEntity = new HttpEntity<String>()
        ResponseEntity entity = new RestTemplate().postForEntity("http://localhost:88/rty/user/user1/applications/ZZZ78R/users/409?username=ABCedit", httpEntity, String.class)

        then:
        entity.statusCode == HttpStatus.CREATED
    }
}

【问题讨论】:

    标签: spring groovy spock


    【解决方案1】:

    在您的测试运行程序(命令行、IDE、Maven/Gradle)中,您可以设置

    • 环境变量或
    • Java 系统属性

    然后您可以在测试中对其进行评估。例如,这些变量/属性之一可能包含

    • 基础 URL 取决于环境(本地执行、开发系统、集成测试系统、验收测试系统等)或
    • 将根据此变量设置符号名称,如“local”、“development”、“integration”、“acceptance”和不同类型的环境特定变量,如 URL、用户名等。

    我建议使用名为test.envtest_env 或类似名称的Java 系统属性,并结合Spock 基类对其进行评估并设置测试所需的其他属性。基本 URL 将是其中之一。您的所有测试都可以扩展这个基类并受益于预配置的属性,能够在测试执行期间在必要时方便地访问它们。

    【讨论】:

      猜你喜欢
      • 2016-02-21
      • 1970-01-01
      • 2018-12-09
      • 2021-08-09
      • 2015-05-26
      • 1970-01-01
      • 2014-05-21
      • 1970-01-01
      • 2018-06-10
      相关资源
      最近更新 更多