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

Deadly 提交于 2021-02-08 05:09:29

问题


From Grails 3.1.9 relase notes:

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

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

Then in my application.yml I had something like:

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

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?

Solution

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"
    }
}

回答1:


You can use the following in your integration test.

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

This will give you the port in the test



来源:https://stackoverflow.com/questions/38252059/how-to-get-the-url-where-grails-3-1-9-integration-tests-are-running

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!