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: 4.6.3

1. Getting Started

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

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:

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=maven --lang=kotlin
If you don’t specify the --build argument, Gradle with the Kotlin DSL is used as the build tool.
If you don’t specify the --lang argument, Java is used as the language.
If you don’t specify the --test argument, JUnit is used for Java and Kotlin, and Spock is used for Groovy.

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/kotlin/example/micronaut/HelloController.kt
package example.micronaut

import io.micronaut.http.MediaType
import io.micronaut.http.annotation.Controller
import io.micronaut.http.annotation.Get
import io.micronaut.http.annotation.Produces

@Controller("/hello") (1)
class HelloController {

    @Get(2)
    @Produces(MediaType.TEXT_PLAIN) (3)
    fun index() = "Hello World"
}
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 with the @Produces annotation.
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:

pom.xml
<dependency>
    <groupId>io.micronaut.test</groupId>
    <artifactId>micronaut-test-rest-assured</artifactId>
    <scope>test</scope>
</dependency>

7. Test

src/test/kotlin/example/micronaut/HelloControllerTest.kt
package example.micronaut

import io.micronaut.test.extensions.junit5.annotation.MicronautTest
import io.restassured.specification.RequestSpecification
import org.junit.jupiter.api.Test

import org.hamcrest.CoreMatchers.`is`

@MicronautTest(1)
class HelloControllerTest {

    @Test
    fun testHelloEndpoint(spec: RequestSpecification) {(2)
        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:

./mvnw test

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.

11. License

All guides are released with an Apache license 2.0 license for the code and a Creative Commons Attribution 4.0 license for the writing and media (images…​).