Java / Gradle

1. Testing Serialization - Spring Boot vs Micronaut Framework - Building a REST API

This guide compares how to test serialization and deserialization with Micronaut Framework and Spring Boot.

Sergio del Amo
On this guide
In this section

Sample Project

You can download a sample application with the code examples in this article.

Introduction

This guide compares how to test serialization in Micronaut Framework and Spring Boot applications.

This guide is the first tutorial of Building a REST API - a series of tutorials comparing how to develop a REST API with Micronaut Framework and Spring Boot.

Record

The application you will build is a REST API for Software as a Service (SaaS) subscriptions. The application serializes and deserializes the following Java record from and to JSON.

springboot/java/src/main/java/example/micronaut/SaasSubscription.java
package example.micronaut;

record SaasSubscription(Long id, String name, Integer cents) {
}

Micronaut supports serialization with Jackson Databind or with Micronaut Serialization. If you use Micronaut Jackson Databind, the above record will be identical in Micronaut Framework and Spring Boot.

Micronaut Serialization

If you use Micronaut Serialization, you must opt the class in to serialization, which you can do by annotating it with @Serdeable.

micronautframeworkserde/java/src/main/java/example/micronaut/SaasSubscription.java

Expected JSON

We want deserialization from a JSON object into the Java record:

micronautframeworkjacksondatabind/java/src/test/resources/expected.json
{
  "id": 99,
  "name": "Professional",
  "cents": 4900
}

In the Micronaut application, we add this test resource to src/test/resources/. In the Spring Boot application, we add it to src/test/resources/example/micronaut. Both applications use example/micronaut as the app’s package.

Tests

In this tutorial, we use AssertJ in the tests. Moreover, we use Jayway JsonPath - a Java DSL for reading JSON documents.

Spring Boot Test

Because creating an application context in Spring Boot is slow, integration tests in Spring Boot use Test Slicing. When using test slicing, Spring creates a reduced application context for a specific application slice.

The following tests verify serialization and deserialization:

springboot/java/src/test/java/example/micronaut/SaasSubscriptionJsonTest.java

Micronaut Framework Test

Micronaut application context startup is fast. In your tests, you will not employ test slicing. Instead, you will use @MicronautTest. The only customization you can specify with @MicronautTest is whether to start an embedded server or not.

micronautframeworkjacksondatabind/java/src/test/java/example/micronaut/SaasSubscriptionJsonTest.java

Conclusion

As shown in this tutorial, testing serialization in Micronaut Framework and Spring Boot is similar. The main difference is that test slicing is not required in Micronaut Framework.

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).