Skip to content

Latest commit

 

History

History
256 lines (206 loc) · 9.63 KB

README.md

File metadata and controls

256 lines (206 loc) · 9.63 KB

Fixture Monkey

Maven version Build GitHub license

Designed by SeongIn Hong

"Write once, Test anywhere"

Fixture Monkey is a Java & Kotlin library designed to generate controllable arbitrary test objects. Its most distinctive feature is the ability to freely access and configure any nested fields through path-based expressions.

It focuses on simplifying test writing by facilitating the generation of necessary test fixtures. Make your JVM tests more concise and safe with Fixture Monkey.

Table of Contents

Quick Start

Add Fixture Monkey to your project:

Gradle

// Java
testImplementation("com.navercorp.fixturemonkey:fixture-monkey-starter:1.1.11")

// Kotlin
testImplementation("com.navercorp.fixturemonkey:fixture-monkey-starter-kotlin:1.1.11")

Maven

<!-- Java -->
<dependency>
    <groupId>com.navercorp.fixturemonkey</groupId>
    <artifactId>fixture-monkey-starter</artifactId>
    <version>1.1.11</version>
    <scope>test</scope>
</dependency>

<!-- Kotlin -->
<dependency>
    <groupId>com.navercorp.fixturemonkey</groupId>
    <artifactId>fixture-monkey-starter-kotlin</artifactId>
    <version>1.1.11</version>
    <scope>test</scope>
</dependency>

Create your first test object:

// Java
FixtureMonkey fixtureMonkey = FixtureMonkey.create();
Product product = fixtureMonkey.giveMeOne(Product.class);

// Kotlin
val fixtureMonkey = FixtureMonkey.create()
val product = fixtureMonkey.giveMeOne<Product>()

Java Example

@Test
void sampleOrder() {
    FixtureMonkey sut = FixtureMonkey.builder()
            .objectIntrospector(ConstructorPropertiesArbitraryIntrospector.INSTANCE)
            .build();

    Order actual = sut.giveMeBuilder(Order.class)
            .set(javaGetter(Order::getOrderNo), "1")
            .set(javaGetter(Order::getProductName), "Line Sally")
            .minSize(javaGetter(Order::getItems), 1)
            .sample();

    then(actual.getOrderNo()).isEqualTo("1");
    then(actual.getProductName()).isEqualTo("Line Sally");
    then(actual.getItems()).hasSizeGreaterThanOrEqualTo(1);
}

Kotlin Example

@Test
fun sampleOrder() {
    val sut = FixtureMonkey.builder()
            .plugin(KotlinPlugin())
            .build()

    val actual = sut.giveMeBuilder<Order>()
            .setExp(Order::orderNo, "1")
            .setExp(Order::productName, "Line Sally")
            .minSizeExp(Order::items, 1)
            .sample()

    then(actual.orderNo).isEqualTo("1")
    then(actual.productName).isEqualTo("Line Sally")
    then(actual.items).hasSizeGreaterThanOrEqualTo(1)
}

Note: Add "lombok.anyConstructor.addConstructorProperties=true" in lombok.config when using Lombok with Fixture Monkey

Why use Fixture Monkey?

1. One-Line Test Object Generation

// Before: Manual object creation
Product product = new Product();
product.setId(1L);
product.setName("Test Product");
// ... many more setters

// After: With Fixture Monkey
Product product = fixtureMonkey.giveMeOne(Product.class);

Stop writing boilerplate code for test object creation. Generate any test object with a single line of code.

2. Intuitive Path-Based Configuration

// Set all product names to "Special Product" with a single expression
ArbitraryBuilder<Order> orderBuilder = fixtureMonkey.giveMeBuilder(Order.class)
    .set("items[*].product.name", "Special Product");

Bid farewell to endless getter/setter chains. Path expressions let you configure any nested field with a single line.

3. Reusable Test Specifications

// Define once, reuse everywhere
ArbitraryBuilder<Product> productBuilder = fixtureMonkey.giveMeBuilder(Product.class)
    .set("category", "Book")
    .set("price", 1000);

// Reuse in different tests
Product product1 = productBuilder.sample();  
Product product2 = productBuilder.size("reviews", 3).sample();

Eliminate test code duplication by defining specifications once and reusing them across your test suite.

4. Universal Object Generation

// Handles inheritance, circular references, and complex structures
Foo foo = FixtureMonkey.create().giveMeOne(Foo.class);  // even with circular references
Bar bar = FixtureMonkey.create().giveMeOne(Bar.class);  // even with inheritance

From simple POJOs to complex object graphs, Fixture Monkey handles all object structures.

5. Dynamic Test Data

// Each sample generates unique test data
Product sample1 = fixtureMonkey.giveMeBuilder(Product.class).sample();
Product sample2 = fixtureMonkey.giveMeBuilder(Product.class).sample();
assertThat(sample1).isNotEqualTo(sample2);

Move beyond static test data to discover edge cases that static data might miss.

Real Test Example

@Test
void testOrderProcessing() {
    // Given
    Order order = fixtureMonkey.giveMeBuilder(Order.class)
        .set("items[*].quantity", 2)
        .set("items[*].product.price", 1000)
        .sample();
    
    // When
    OrderResult result = new OrderProcessor().process(order);
    
    // Then
    assertThat(result.getTotalAmount()).isEqualTo(4000); // 2 items * 2 quantity * 1000 price
    assertThat(result.getStatus()).isEqualTo(OrderStatus.COMPLETED);
}

Requirements

  • JDK 1.8 or higher
  • Jqwik 1.7.3
  • Kotlin 1.8 or higher (for Kotlin support)
  • kotest-property 5.9.1 (for Kotlin support)

Battle-Tested in Production

Originally developed at Naver, Fixture Monkey has proven its reliability in handling complex business requirements at scale, supporting over 10,000 tests for South Korea's leading mobile payment service.

Documentation

Additional Resources

Tools and Plugins

Articles

Welcome to write articles about Fixture Monkey! Please make an issue to let us know if you'd like to share your post.

Contributors

Thanks to all contributors

License

Copyright 2021-present NAVER Corp.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
    http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.