Over a million developers have joined DZone.

Grails Goodness: Use Random Server Port in Integration Tests

DZone's Guide to

Grails Goodness: Use Random Server Port in Integration Tests

In Grails 3, we can leverage Spring Boot to run integration tests across random available port numbers. Check out how make it happen in this article!

· DevOps Zone
Free Resource

Download “The DevOps Journey - From Waterfall to Continuous Delivery” to learn learn about the importance of integrating automated testing into the DevOps workflow, brought to you in partnership with Sauce Labs.

Because Grails 3 is based on Spring Boot we can use a lot of the functionality of Spring Boot in our Grails applications. For example we can start Grails 3 with a random available port number, which is useful in integration testing scenarios. To use a random port we must set the application property server.port to the value 0. If we want to use the random port number in our code we can access it via the @Value annotation with the expression ${local.server.port}.

Let's create a very simple controller with a corresponding integration test. The controller is called Sample:

// File: grails-app/controllers/mrhaki/SampleController.groovy
package mrhaki

class SampleController {

 def index() { 
 respond([message: 'Grails 3 is awesome!'])

We write a Spock integration test that will start Grails and we use the HTTP Requests library to access the /sample endpoint of our application.

// File: src/integration-test/groovy/mrhaki/SampleControllerIntSpec.groovy
package mrhaki

import com.budjb.httprequests.HttpClient
import com.budjb.httprequests.HttpClientFactory
import grails.test.mixin.integration.Integration
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.beans.factory.annotation.Value
import spock.lang.Specification

class SampleControllerIntSpec extends Specification {

 * Server port configuration for Grails test 
 * environment is set to server.port = 0, which
 * means a random available port is used each time
 * the application starts.
 * The value for the port number is accessible
 * via ${local.server.port} in our integration test.
 Integer serverPort

 * HTTP test client from the HTTP Requests library:
 * http://budjb.github.io/http-requests/latest
 HttpClientFactory httpClientFactory

 private HttpClient client

 def setup() {
 // Create HTTP client for testing controller.
 client = httpClientFactory.createHttpClient()

 void "sample should return JSON response"() {
 // Nice DSL to build a request.
 def response = client.get {
 // Here we use the serverPort variable.
 uri = "http://localhost:${serverPort}/sample"
 accept = 'application/json'

 response.status == 200

 final Map responseData = response.getEntity(Map)
 responseData.message == 'Grails 3 is awesome!'

Finally we add the server.port to the application configuration:

# File: grails-app/conf/application.yml
 port: 0 # Random available port

Let's run the integration test: $ grails test-app mrhaki.SampleControllerIntSpec -integration. When we open the test report and look at the standard output we can see that a random port is used:

Written with Grails 3.1.6.

Discover how to optimize your DevOps workflows with our cloud-based automated testing infrastructure, brought to you in partnership with Sauce Labs

grails ,integration test ,random ,server ,port

Published at DZone with permission of Hubert Klein Ikkink, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.


Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.


{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}