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.
- Quick Start
- Why use Fixture Monkey?
- Real Test Example
- Requirements
- Documentation
- Additional Resources
- Contributors
- License
Add Fixture Monkey to your project:
// Java
testImplementation("com.navercorp.fixturemonkey:fixture-monkey-starter:1.1.11")
// Kotlin
testImplementation("com.navercorp.fixturemonkey:fixture-monkey-starter-kotlin:1.1.11")
<!-- 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>()
@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);
}
@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
// 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.
// 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.
// 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.
// 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.
// 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.
@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);
}
- 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)
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.
- FixtureMonkey Helper - IntelliJ plugin for easier Fixture Monkey usage
- fixure monkey로 예외 발생 테스트
- 테스트 객체를 더쉽게 만들어보자, Fixture-monkey
- Junit Test with Fixture Monkey
- Fixture monkey
- 테스트 데이터도구 - Fixture Monkey
- Fixture Monkey란?
- 테스트를 작성할 수 밖에 없는 사람들에게
- Fixture Monkey 사용해보기
- Simplify Unit Testing with Fixture Monkey
- Getting Started Easy Test Fixture Customization with Fixture Monkey
- [Fixture Monkey] 픽스쳐 몽키로 테스트 코드 작성하기 (Java Spring)
- TestFixture를 쉽게 생성해 주는 라이브러리가 있다?
- Fixture Monkey로 테스트 픽스처를 쉽게 생성하고 리팩토링 해보자
- Fixture Monkey를 적용해보자 w/JPA Test
Welcome to write articles about Fixture Monkey! Please make an issue to let us know if you'd like to share your post.
- 🐒 ah.jo
- 🐒 mhyeon-lee
- 🐒 acktsap
- 🐒 benelog
- 🐒 jwChung
- 🐒 SooKim1110
Thanks to all contributors
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.