Quick Start Guide
What You'll Learn
- Core methods for customizing test objects with Fixture Monkey
- Basic approaches to customize simple and complex objects
- Solutions to the most common problems beginners face
5-Minute Quick Start
This section covers only the essential information needed to get started with Fixture Monkey.
4 Key Methods You Must Know
If you're short on time, here's what you need to know right now. First, create a FixtureMonkey instance:
FixtureMonkey fixtureMonkey = FixtureMonkey.create();
Then use these core methods:
- Java
- Kotlin
// given
Product product = fixtureMonkey.giveMeBuilder(Product.class)
.set("name", "Smartphone")
.set("price", new BigDecimal(499))
.sample();
Order order = fixtureMonkey.giveMeBuilder(Order.class)
.size("products", 2)
.set("products[0].name", "Laptop")
.sample();
// then
then(product.getName()).isEqualTo("Smartphone");
then(order.getProducts()).hasSize(2);
// given
val product = fixtureMonkey.giveMeBuilder<Product>()
.set("name", "Smartphone")
.set("price", BigDecimal(499))
.sample()
val order = fixtureMonkey.giveMeBuilder<Order>()
.size("products", 2)
.set("products[0].name", "Laptop")
.sample()
// then
then(product.name).isEqualTo("Smartphone")
then(order.products).hasSize(2)
Visual Overview
Here's a simple flowchart showing the process of customizing objects with Fixture Monkey:
Prerequisites
This guide assumes:
- You've already added Fixture Monkey to your project
- You know how to create a basic FixtureMonkey instance
If you haven't set up Fixture Monkey yet, refer to the Getting Started section first.
Basic Customization Methods
This section introduces the most fundamental customization methods you'll use daily.
Setting Property Values
The most basic way to customize an object is to set specific property values:
- Java
- Kotlin
Product product = fixtureMonkey.giveMeBuilder(Product.class)
.set("name", "Smartphone")
.set("price", new BigDecimal("499.99"))
.set("available", true)
.sample();
then(product.getName()).isEqualTo("Smartphone");
val product = fixtureMonkey.giveMeBuilder<Product>()
.set("name", "Smartphone")
.set("price", BigDecimal("499.99"))
.set("available", true)
.sample()
then(product.name).isEqualTo("Smartphone")
Setting Null Values
When you need to test with null values:
- Java
- Kotlin
Product nullNameProduct = fixtureMonkey.giveMeBuilder(Product.class)
.setNull("name")
.sample();
then(nullNameProduct.getName()).isNull();
val nullNameProduct = fixtureMonkey.giveMeBuilder<Product>()
.setNull("name")
.sample()
then(nullNameProduct.name).isNull()
Working with Collections
The most important thing when working with collections is to set the size first:
- Java
- Kotlin
Order orderWith2Products = fixtureMonkey.giveMeBuilder(Order.class)
.size("products", 2)
.set("products[0].name", "Laptop")
.sample();
then(orderWith2Products.getProducts()).hasSize(2);
then(orderWith2Products.getProducts().get(0).getName()).isEqualTo("Laptop");
val orderWith2Products = fixtureMonkey.giveMeBuilder<Order>()
.size("products", 2)
.set("products[0].name", "Laptop")
.sample()
then(orderWith2Products.products).hasSize(2)
then(orderWith2Products.products[0].name).isEqualTo("Laptop")
For more advanced collection customization, check the Path Expressions document.
Customizing Nested Objects
You can access nested properties using dot notation:
- Java
- Kotlin
Customer customer = fixtureMonkey.giveMeBuilder(Customer.class)
.set("name", "John Doe")
.set("address.street", "123 Main Street")
.set("address.city", "New York")
.sample();
then(customer.getName()).isEqualTo("John Doe");
then(customer.getAddress().getStreet()).isEqualTo("123 Main Street");
val customer = fixtureMonkey.giveMeBuilder<Customer>()
.set("name", "John Doe")
.set("address.street", "123 Main Street")
.set("address.city", "New York")
.sample()
then(customer.name).isEqualTo("John Doe")
then(customer.address.street).isEqualTo("123 Main Street")
For more complex nested object customization, check the InnerSpec guide.
Frequently Asked Questions
The most common issues beginners face.
Why is my collection empty when I tried to customize an element?
Setting a collection element without specifying the size first (e.g., .set("products[0].name", "Laptop") without .size(...)) may result in an empty collection, causing the element to be ignored.
Always set the collection size before customizing its elements.
Solution:
- Java
- Kotlin
Order orderCorrect = fixtureMonkey.giveMeBuilder(Order.class)
.size("products", 1)
.set("products[0].name", "Laptop")
.sample();
then(orderCorrect.getProducts().get(0).getName()).isEqualTo("Laptop");
val orderCorrect = fixtureMonkey.giveMeBuilder<Order>()
.size("products", 1)
.set("products[0].name", "Laptop")
.sample()
then(orderCorrect.products[0].name).isEqualTo("Laptop")
Why do I get null values when I didn't set them to null?
By default, Fixture Monkey may generate null values for some properties. To ensure values are not null:
- Java
- Kotlin
Product nonNullProduct = fixtureMonkey.giveMeBuilder(Product.class)
.setNotNull("name")
.setNotNull("price")
.sample();
then(nonNullProduct.getName()).isNotNull();
then(nonNullProduct.getPrice()).isNotNull();
val nonNullProduct = fixtureMonkey.giveMeBuilder<Product>()
.setNotNull("name")
.setNotNull("price")
.sample()
then(nonNullProduct.name).isNotNull()
then(nonNullProduct.price).isNotNull()
Next Steps
Now that you've learned the basics, explore these topics for more advanced usage:
- Path Expressions - Accessing and customizing nested properties
- Customization APIs - Complete list of customization methods
- Testing Interfaces - How to work with interfaces
- InnerSpec - Advanced customization for complex objects
- Arbitrary - Generating test data with specific constraints