Goodbye Grails, Hello Micronaut #5: Marshalling

Vladimír Oraný
Stories by Agorapulse
2 min readAug 3, 2021

This is the fifth post in a series that will guide you through the migration from Grails to Micronaut. This guide requires your application to be based on Grails 4.x or later.

Controllers are responsible for the communication with other applications, including your frontend. We need to ensure that your API won't change and the application will be able to consume the same inputs and produce the same outputs as before the migration. Gru testing framework is exactly the tool we are looking for. Gru evaluates the responses from the controllers and it supports Grails and Micronaut out of the box.

You can add Gru into your project by providing the following dependency in your application's subproject Gradle file:

testCompile 'com.agorapulse:gru-grails:0.9.2'

Let's image a very simple controller which only renders a single entity.

We can write a simple test that will validate the JSON output of the controller.

We are using the dataset created in a previous step to load the test data to be rendered by the controller. The file vehicle.json is created automatically on the first run but you need to re-evaluate its contents to ensure there are no variable values such as timestamps. See the reference documentation for advanced use cases such as ignoring timestamps.

Once the current output is covered with the tests, we can switch the internals to ObjectMapper.

In our case, the VehicleResponse looks like a bare Vehicle entity:

The point is to ensure no Grails-related marshalling is happening under the hood. This will help us to switch to Micronaut controllers later as well as to Micronaut Data.

The current tests will fail because of the missing objectMapper bean. Luckily, there is an easy fix using doWithSpring method where you can simply declare the ObjcetMapper bean.

In the next step, we will extract the domain classes into their own library.

Table of Contents

  1. Multiproject
  2. Configuration
  3. Static Compilation
  4. Datasets
  5. Marshalling
  6. Domain Classes
  7. Services
  8. Controllers
  9. Micronaut Application
  10. Micronaut Data

Sources & Discussion

--

--