Skip to main content
Version: v1.2.x

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:

JavakeyMethods()
QuickStartGuideTest.java
// 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);

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:

JavasettingPropertyValues()
QuickStartGuideTest.java
Product product = fixtureMonkey.giveMeBuilder(Product.class)
.set("name", "Smartphone")
.set("price", new BigDecimal("499.99"))
.set("available", true)
.sample();

then(product.getName()).isEqualTo("Smartphone");

Setting Null Values​

When you need to test with null values:

JavasettingNullValues()
QuickStartGuideTest.java
Product nullNameProduct = fixtureMonkey.giveMeBuilder(Product.class)
.setNull("name")
.sample();

then(nullNameProduct.getName()).isNull();

Working with Collections​

The most important thing when working with collections is to set the size first:

JavaworkingWithCollections()
QuickStartGuideTest.java
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");

For more advanced collection customization, check the Path Expressions document.

Customizing Nested Objects​

You can access nested properties using dot notation:

JavacustomizingNestedObjects()
QuickStartGuideTest.java
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");

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?​

Common Mistake

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:

JavacollectionSizeFirst()
QuickStartGuideTest.java
Order orderCorrect = fixtureMonkey.giveMeBuilder(Order.class)
.size("products", 1)
.set("products[0].name", "Laptop")
.sample();

then(orderCorrect.getProducts().get(0).getName()).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:

JavaensureNotNull()
QuickStartGuideTest.java
Product nonNullProduct = fixtureMonkey.giveMeBuilder(Product.class)
.setNotNull("name")
.setNotNull("price")
.sample();

then(nonNullProduct.getName()).isNotNull();
then(nonNullProduct.getPrice()).isNotNull();

Next Steps​

Now that you've learned the basics, explore these topics for more advanced usage:

  1. Path Expressions - Accessing and customizing nested properties
  2. Customization APIs - Complete list of customization methods
  3. Testing Interfaces - How to work with interfaces
  4. InnerSpec - Advanced customization for complex objects
  5. Arbitrary - Generating test data with specific constraints