Features
The Jackson plugin integrates Jackson with Fixture Monkey, giving you:
JacksonObjectArbitraryIntrospectoras the default introspector — creates objects using Jackson's ObjectMapper- Jackson annotation support —
@JsonProperty,@JsonIgnore,@JsonTypeInfo,@JsonSubTypes - Custom ObjectMapper — use your production ObjectMapper configuration
When to use the Jackson Plugin
The Jackson plugin is a good choice when:
- Your classes use Jackson annotations (
@JsonProperty,@JsonIgnore, etc.) - You have a mix of Java and Kotlin classes in your project
- Your classes don't have a no-args constructor or Lombok annotations
- You want object creation to match your production serialization behavior
tip
If your classes have a no-args constructor with setters, the default BeanArbitraryIntrospector works fine without this plugin.
For Lombok @Value classes, consider ConstructorPropertiesArbitraryIntrospector instead.
Dependencies
Gradle
testImplementation("com.navercorp.fixturemonkey:fixture-monkey-jackson:1.1.16")
Maven
<dependency>
<groupId>com.navercorp.fixturemonkey</groupId>
<artifactId>fixture-monkey-jackson</artifactId>
<version>1.1.16</version>
<scope>test</scope>
</dependency>
Plugin Setup
- Java
- Kotlin
FixtureMonkey fixtureMonkey = FixtureMonkey.builder()
.plugin(new JacksonPlugin())
.build();
val fixtureMonkey = FixtureMonkey.builder()
.plugin(KotlinPlugin())
.plugin(JacksonPlugin())
.build()
note
For Kotlin projects, add both KotlinPlugin() and JacksonPlugin(). Also add jackson-module-kotlin to your dependencies for proper Kotlin class serialization.
Using a Custom ObjectMapper
If your production code uses a custom ObjectMapper, pass it to the plugin to ensure test objects match your serialization configuration:
- Java
- Kotlin
ObjectMapper objectMapper = JsonMapper.builder()
.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES)
.build();
FixtureMonkey fixtureMonkey = FixtureMonkey.builder()
.plugin(new JacksonPlugin(objectMapper))
.build();
val objectMapper = JsonMapper.builder()
.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES)
.build()
val fixtureMonkey = FixtureMonkey.builder()
.plugin(KotlinPlugin())
.plugin(JacksonPlugin(objectMapper))
.build()
Quick Example
- Java
- Kotlin
@Value
public class Product {
long id;
@JsonProperty("name")
String productName;
long price;
@JsonIgnore
List<String> options;
}
@Test
void test() {
FixtureMonkey fixtureMonkey = FixtureMonkey.builder()
.plugin(new JacksonPlugin())
.build();
Product product = fixtureMonkey.giveMeBuilder(Product.class)
.set("name", "book") // uses @JsonProperty name
.sample();
then(product.getProductName()).isEqualTo("book");
then(product.getOptions()).isNull(); // @JsonIgnore
}
data class Product(
val id: Long,
@JsonProperty("name")
val productName: String,
val price: Long,
@JsonIgnore
val options: List<String>? = null
)
@Test
fun test() {
val fixtureMonkey = FixtureMonkey.builder()
.plugin(KotlinPlugin())
.plugin(JacksonPlugin())
.build()
val product = fixtureMonkey.giveMeBuilder<Product>()
.set("name", "book") // uses @JsonProperty name
.sample()
then(product.productName).isEqualTo("book")
then(product.options).isNull() // @JsonIgnore
}
Learn More
- JacksonObjectArbitraryIntrospector — how the introspector works and performance considerations
- Jackson Annotations — supported annotations and examples