DZone
DevOps Zone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone > DevOps Zone > Grails Goodness: Use Random Server Port in Integration Tests

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!

Hubert Klein Ikkink user avatar by
Hubert Klein Ikkink
·
May. 17, 16 · DevOps Zone · Tutorial
Like (2)
Save
Tweet
2.15K Views

Join the DZone community and get the full member experience.

Join For Free

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

@Integration
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.
 */
 @Value('${local.server.port}')
 Integer serverPort

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

 private HttpClient client

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

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

 then:
 response.status == 200

 and:
 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
...
environments:
 test:
 server:
 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.

Grail (web browser) integration test

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

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • 10 Books Every Senior Engineer Should Read
  • Waterfall Vs. Agile Methodologies: Which Is Best For Project Management?
  • How to Determine if Microservices Architecture Is Right for Your Business
  • Making Your SSR Sites 42x Faster With Redis Cache

Comments

DevOps Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo