从Grails的3.1.9 RELASE笔记:如何获得运行Grails 3.1.9集成测试的Url?
集成测试端口 集成和功能测试,现在一个随机端口 而不是相同的端口默认情况下,应用程序上运行。 这是为了避免应用程序 已在运行时出现端口冲突问题。集成测试是硬编码端口 将需要进行修改,以使用的标有@Integration
我通常与
import grails.util.Holders ... .. . def urlWhereIntegrationAppIsRunning = Holders.grailsApplication.config.grails.serverURL
获得集成测试网址,然后测试 serverPort财产在我的 application.yml
我有类似的东西:
test: server: port: 8090 grails: serverURL: http://localhost:8090
如何在集成测试中使用Grails 3.19运行集成测试的Url(服务器url和随机端口)?
解决方案
基于马可的答案,我已经做了特质封装的URL集成测试在何处运行。我在测试我的API的Spock规范中实现了这个特性。
import org.springframework.beans.factory.annotation.Value
trait TIntegrationServerUrl {
@Value('${local.server.port}')
Integer serverPort
String integrationServerUrl() {
"http://localhost:$serverPort"
}
}