如何获取正在运行Grails 3.1.9集成测试的Url? [英] How to get the Url where Grails 3.1.9 integration tests are running?

查看:62
本文介绍了如何获取正在运行Grails 3.1.9集成测试的Url?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从Grails 3.1.9发布注意事项:

From Grails 3.1.9 relase notes:

集成测试端口集成和功能测试现在可以在随机端口上运行而不是默认情况下与应用程序位于同一端口.这是为了避免应用程序在端口冲突时出现问题已经开始.对端口进行硬编码的集成测试将需要更改以使用标有@Integration的测试的serverPort属性

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

我通常使用的是集成测试网址

I was normally getting the integration test url with

导入grails.util.Holders......def urlWhereIntegrationAppIsRunning = Holders.grailsApplication.config.grails.serverURL

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

Then in my application.yml I had something like:

<代码>测试:服务器:端口:8090grails:serverURL:http://localhost:8090

如何在集成测试中使用Grails 3.19运行集成测试的Url(服务器URL和随机端口)?

How can I get the Url (server url and random port) where my integration test is running with Grails 3.19 inside an integration test?

基于Marco的回答,我完成了一个Trait来封装运行集成测试的URL.我在测试我的API的Spock规范中实现了此特征.

Based on Marco's answer I have done a Trait to encapsulate the url where the integration test run. I implement this trait in the Spock's specifications where I test my API.

import org.springframework.beans.factory.annotation.Value

trait TIntegrationServerUrl {

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

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

推荐答案

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

You can use the following in your integration test.

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

这将为您提供测试中的端口

This will give you the port in the test

这篇关于如何获取正在运行Grails 3.1.9集成测试的Url?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆