如何获得 Grails 3.1.9 集成测试 运行 的 Url?

How to get the Url where Grails 3.1.9 integration tests are running?

来自 Grails 3.1.9 发行说明:

Integration Test Port Integration and functional tests now run on a random port instead of the same port as the application by default. This is to avoid port conflict problems when the application is already running. Integration tests that hard code the port will need to be altered to use the serverPort property of tests marked with @Integration

我通常通过

进行集成测试 url

import grails.util.Holders ... .. . def urlWhereIntegrationAppIsRunning = Holders.grailsApplication.config.grails.serverURL

然后在我的 application.yml 我有类似的东西:

test: server: port: 8090 grails: serverURL: http://localhost:8090

如何在集成测试中获取 Url(服务器 url 和随机端口)运行 Grails 3.19?

解决方案

根据 Marco 的回答,我做了一个 Trait 来封装集成测试 运行 的 url。我在我测试 API.

的 Spock 规范中实现了这个特性
import org.springframework.beans.factory.annotation.Value

trait TIntegrationServerUrl {

    @Value('${local.server.port}')
    Integer serverPort

    String integrationServerUrl() {
        "http://localhost:$serverPort"
    }
}

您可以在集成测试中使用以下内容。

@Value('${local.server.port}')
Integer port

这会给你测试中的端口