Groovier Spring Boot Integration Testing

Recently I had a chance to use Groovy’s groovy.json.JsonSlurper in a soapUI REST project. The usage scenario in soapUI (in particular, soapUI Open Source) was very simple: in a Groovy assertion, load the content of a response into a variable and than verify it like this:

import groovy.json.JsonSlurper;

def slurper = new JsonSlurper()
def result = slurper.parseText(messageExchange.responseContent)

assert result.incidentType == 'Lorem ipsum'

This works well in soapUI. But what about Spring Boot integration tests? Can I make integration testing of REST API groovier?

API to be tested

For the purpose of this article I used existing Spring Boot Jersey Demo application:

Adding Groovy to Spring Boot project

Add Groovy and HTTPBuilder (Easy HTTP client for Groovy) dependencies.

testCompile 'org.codehaus.groovy:groovy-all:2.4.3'
testCompile 'org.codehaus.groovy.modules.http-builder:http-builder:0.7.1'

All Groovy tests will be kept in src/test/groovy folder.

First integration test

The first test will verify /customer endpoint:

class FindAllCustomersGroovyTest {

    RESTClient client = new RESTClient("http://localhost:9000")

    def void before() {
        client.auth.basic("demo", "123")

    def void findsAll() {
        def response = client.get(path: "/customer")

        def json = response.responseData

        assert json.number == 0
        assert json.totalPages == 1
        assert json.totalElements == 3
        assert json.content.size() == 3
        assert json.content.firstname.any() {
            firstname ->

  • @ApplicationTest – is a grouping annotation that wraps several Spring’s annotation. Groovy tests can be annotated with any JUnit and/or Spring annotations.
@SpringApplicationConfiguration(classes = Application.class)
@Sql(scripts = "classpath:data.sql", executionPhase = Sql.ExecutionPhase.BEFORE_TEST_METHOD)
public @interface ApplicationTest {

  • HTTPBuilder module provides an RESTClient extension that allows querying REST API with ease. HTTPBuilder will be used instead of Spring Boot’s TestRestClient.
  • API requires Basic Auth and configuring it with RESTClient is very easy. For more complex scenarios, it allows more sophisticated authentication mechanisms to be used.
  • RESTClient response contains responseData that is JSONSlurper parsed object. And since we are groovy, asserting content of the response is very easy.

Sending JSON content

Let’s see how to send JSON data along with the request:

class SaveCustomerGroovyTest {

    public void savesCustomer() {
        def response =
                path: '/customer',
                requestContentType: "application/json",
                body: [
                        firstname: "John",
                        lastname: "Doe",
                                [value: ""]

        assert 201 == response.status
        assert response.headers.location


Note: No further explanation is needed, I believe.

Reference: Groovier Spring Boot Integration Testing from our JCG partner Rafal Borowiec at the blog.

Want to know how to develop your skillset to become a Java Rockstar?

Join our newsletter to start rocking!

To get you started we give you our best selling eBooks for FREE!


1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design


and many more ....


Receive Java & Developer job alerts in your Area

I have read and agree to the terms & conditions


Rafal Borowiec

Software developer, Team Leader, Agile practitioner, occasional blogger, lecturer. Open Source enthusiast, quality oriented and open-minded.
Notify of

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Inline Feedbacks
View all comments
Back to top button