Sergio del Amo
Sergio del Amo

Reputation: 78106

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

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

Upvotes: 2

Views: 746

Answers (1)

Marco
Marco

Reputation: 15929

You can use the following in your integration test.

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

This will give you the port in the test

Upvotes: 5

Related Questions