unit-test-json-serialization
Provides patterns for unit testing JSON serialization/deserialization with Jackson and `@JsonTest`. Validates JSON mapping, custom serializers, date formats, and polymorphic types. Use when testing JSON serialization, validating custom serializers, or writing JSON unit tests in Spring Boot applications.
Security Assessment
About unit-test-json-serialization
This skill provides patterns for unit testing JSON serialization and deserialization in Spring Boot applications using Spring's @JsonTest slice and Jackson. It solves the problem of verifying that DTOs map correctly to and from JSON, that custom serializers behave as intended, and that field mappings, date formats, and polymorphic types are handled reliably, without spinning up a full application context.
The skill explains the JacksonTester-based workflow step by step: annotate the test class with @JsonTest, autowire a JacksonTester for the target type, assert serialization output with extractingJsonPath* helpers, parse JSON back into objects to assert state, validate round-trips, and cover edge cases like null values, missing fields, empty collections, and invalid JSON. It includes concrete, copy-ready examples for Maven and Gradle setup, basic serialize/deserialize tests, @JsonProperty and @JsonIgnore field mapping, nested objects and lists, LocalDateTime formatting, custom JsonSerializer implementations, and polymorphic deserialization with @JsonTypeInfo and @JsonSubTypes. It also recommends adding validation checkpoints to ensure each assertion fails meaningfully on wrong data.
It targets Java and Spring Boot developers writing or hardening JSON-related unit tests. Typical use cases include testing REST DTO contracts, verifying custom Jackson serializers and deserializers, confirming that ignored fields never leak into output, and validating type discrimination for polymorphic payloads.
FAQ
What testing approach does this skill teach?
It uses Spring's @JsonTest slice with autowired JacksonTester instances to write focused serialization/deserialization unit tests, asserting JSON with extractingJsonPath* helpers and parsing JSON back into objects to check state.
What prerequisites and dependencies are needed?
A Spring Boot project with spring-boot-starter-json and spring-boot-starter-test (Maven or Gradle setup snippets are provided). Tests rely on Jackson and the JacksonTester provided by the @JsonTest auto-configuration.
Which JSON scenarios are covered?
POJO mapping, @JsonProperty/@JsonIgnore field mappings, nested objects and lists, date/time formatting (LocalDateTime, Date), custom serializers, null and missing-field handling, and polymorphic deserialization via @JsonTypeInfo and @JsonSubTypes.
How do I verify a field is never serialized?
Serialize the object with json.write(...) and assert the resulting JSON does not contain the field name, as shown in the @JsonIgnore example for an internal note field.
Does it help ensure tests are meaningful?
Yes. It advises adding validation checkpoints so that after each assertion you confirm the test actually fails when given wrong data, avoiding false-positive green tests.
Install unit-test-json-serialization
Quick Setup:
- Copy the skill folder to
.claude/skills/ - Claude will automatically detect and use the skill
Repository
giuseppe-trisciuoglio/developer-kit