These tutorials target Micronaut Framework 3. Read, Guides for Micronaut Framework 4.

Using Micronaut Test REST-Assured in a Micronaut application

Learn how to use Micronaut Test REST-Assured to test the REST API of your Micronaut application.

Authors: Sergio del Amo, Dean Wette

Micronaut Version: 3.9.2

1. Getting Started

In this guide, we will create a Micronaut application written in Groovy.

2. REST-assured

In this guide, you will test a Micronaut application using REST-assured.

Testing and validating REST services in Java is harder than in dynamic languages such as Ruby and Groovy. REST Assured brings the simplicity of using these languages into the Java domain.

The Micronaut Test REST Assured module makes it easier to integrate the REST-assured library. Using it eliminates the need to hard code the version, and simplifies test fixtures by supporting injection of RequestSpecification into test fields or method parameters (parameters are only supported with JUnit 5):

3. What you will need

To complete this guide, you will need the following:

  • Some time on your hands

  • A decent text editor or IDE

  • JDK 1.8 or greater installed with JAVA_HOME configured appropriately

4. Solution

We recommend that you follow the instructions in the next sections and create the application step by step. However, you can go right to the completed example.

5. Writing the Application

Create an application using the Micronaut Command Line Interface or with Micronaut Launch.

mn create-app example.micronaut.micronautguide --build=gradle --lang=groovy
If you don’t specify the --build argument, Gradle is used as the build tool.
If you don’t specify the --lang argument, Java is used as the language.

The previous command creates a Micronaut application with the default package example.micronaut in a directory named micronautguide.

5.1. Controller

In order to create a microservice that responds with "Hello World" you first need a controller.

Create a Controller:

src/main/groovy/example/micronaut/HelloController.groovy
package example.micronaut

import groovy.transform.CompileStatic
import io.micronaut.http.annotation.Controller
import io.micronaut.http.annotation.Get
import io.micronaut.http.annotation.Produces
import io.micronaut.http.MediaType

@CompileStatic
@Controller("/hello") (1)
class HelloController {
    @Get (2)
    @Produces(MediaType.TEXT_PLAIN) (3)
    String index() {
        "Hello World" (4)
    }
}
1 The class is defined as a controller with the @Controller annotation mapped to the path /hello.
2 The @Get annotation maps the index method to an HTTP GET request on /hello.
3 By default, a Micronaut response uses application/json as Content-Type. We are returning a String, not a JSON object, so we set it to text/plain.
4 A String "Hello World" is returned as the result

6. Dependencies

To use REST-assured, add the following dependency on the Micronaut Test REST-assured module:

build.gradle
testImplementation("io.micronaut.test:micronaut-test-rest-assured")

7. Test

src/test/groovy/example/micronaut/HelloControllerSpec.groovy
package example.micronaut

import io.micronaut.test.extensions.spock.annotation.MicronautTest
import io.restassured.specification.RequestSpecification
import jakarta.inject.Inject
import spock.lang.Specification

import static org.hamcrest.CoreMatchers.is

@MicronautTest (1)
class HelloControllerSpec extends Specification {

    @Inject
    RequestSpecification spec (2)

    void "test hello endpoint"() {
        expect:
        spec    (3)
            .when()
                .get('/hello')
            .then()
                .statusCode(200)
                .body(is('Hello World'))
    }
}
1 Annotate the class with @MicronautTest so the Micronaut framework will initialize the application context and the embedded server. More info.
2 Inject an instance of RequestSpecification.
3 Micronaut Test sets the embedded server port on spec, so it’s unnecessary to inject EmbeddedServer and retrieve it explicitly.

8. Testing the Application

To run the tests:

./gradlew test

Then open build/reports/tests/test/index.html in a browser to see the results.

9. Next steps

Explore more features with Micronaut Guides.

Learn more about Micronaut Test.

10. Help with the Micronaut Framework

The Micronaut Foundation sponsored the creation of this Guide. A variety of consulting and support services are available.