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.
On this guide
In this section
Getting Started
In this guide, we will create a Micronaut application written in Kotlin.
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):
What you will need
To complete this guide, you will need the following:
-
Some time on your hands
-
A decent text editor or IDE (e.g. IntelliJ IDEA)
-
JDK 21 or greater installed with
JAVA_HOMEconfigured appropriately
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.
-
Download and unzip the source
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=kotlin|
Note
|
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.
Controller
To create a microservice that responds with "Hello World", you first need a controller.
Create a controller:
Dependencies
To use REST-assured, add the following dependency on the Micronaut Test REST-assured module:
testImplementation("io.micronaut.test:micronaut-test-rest-assured")Test
Testing the Application
To run the tests:
./gradlew testThen open build/reports/tests/test/index.html in a browser to see the results.
Next Steps
Explore more features with Micronaut Guides.
Learn more about Micronaut Test.
Help with the Micronaut Framework
The Micronaut Foundation sponsored the creation of this Guide. A variety of consulting and support services are available.
License
|
Note
|
All guides are released with an Apache License 2.0 for the code and a Creative Commons Attribution 4.0 license for the writing and media (images). |