Goodbye Grails, Hello Micronaut #4: Datasets

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

This is the fourth 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.

We should cover the crucial parts of the application with tests as things can go wrong very easily during the migration. For the meaningful tests, we will need the meaningful data. In this post, we are going to use Dru framework for this purpose because it already supports Grails and GORM, and Micronaut Data out of the box.

Dru excels in creating the relationships between different entities but we won't get into too many details. You should rather use the documentation for the full reference.

Let's say we have a simple domain class in our codebase:

Vehicle.groovy

If you want to define the dataset for an entity then we usually choose from either a JSON or SQL file. You can use responses from your production or test environment using the JSON source or you can use simplified database dumps with the SQL source. To add Dru on your classpath update the application's Gradle file with the following dependencies:

The following snippet shows using the JSON fixture to load test data:

HelloDataSets.groovy

Given the class HelloDataSet being declared in hello package then the JSON file containing the test data for vehicles is located in src/test/resources/hello/HelloDataSet/vehicles.json file.

vehicles.json

The data set deserves its own specification as a lot of other tests will depend on the data being loaded properly:

HelloDataSetsSpec.groovy

Creating data sets will later help us to create tests for the controllers as well as migrating from GORM to Micronaut Data.

In the next step, we will move towards decoupling the web layer from the domain layer by introducing Data Transfer Objects (DTOs) into the controllers.

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

--

--