Tips for Beginners
Essential Tips for Using Fixture Monkey
1. Use Type-Safe Methods
- Prefer type-safe methods over string-based ones
- Example:
// Instead of
.set("price", 1000L)
// Use
.set(javaGetter(Product::getPrice), 1000L)
2. Use Meaningful Test Data
- Use values that make sense in your test context
- Avoid using arbitrary values like "test" or "123"
- Consider business rules and constraints when setting values
- Benefits:
- Makes tests more readable and self-documenting
- Helps identify test failures more quickly
- Makes it easier to understand test scenarios
- Reduces the need for additional comments
- Example:
JavameaningfulTestData()
TipsTest.javaProduct product = FIXTURE_MONKEY.giveMeBuilder(Product.class)
.set("price", 1000L)
.set("name", "Premium Product")
.set("category", "ELECTRONICS")
.set("stock", 50)
.sample();
then(product).isNotNull();
3. Keep Tests Readable
- Add comments to explain why specific values are set
- Example:
JavareadableTests()
TipsTest.javaProduct product = FIXTURE_MONKEY.giveMeBuilder(Product.class)
.set("price", 2000L)
.set("category", "PREMIUM")
.sample();
then(product).isNotNull();
4. Handle Collections Properly
- Set collection size before accessing specific indices
- Example:
JavahandleCollections()
TipsTest.javaProduct product = FIXTURE_MONKEY.giveMeBuilder(Product.class)
.size("options", 3)
.set("options[1]", "red")
.sample();
then(product.getOptions()).hasSize(3);
then(product.getOptions().get(1)).isEqualTo("red");
5. Reuse FixtureMonkey Instance
- Create one instance and reuse it across tests
- Example:
JavareuseFixtureMonkey()
TipsTest.javaProduct product1 = FIXTURE_MONKEY.giveMeBuilder(Product.class).sample();
Product product2 = FIXTURE_MONKEY.giveMeBuilder(Product.class).sample();
then(product1).isNotNull();
then(product2).isNotNull();
6. Reuse ArbitraryBuilder
- Reuse ArbitraryBuilder instances to maintain consistent test data structure
- Share common configurations across multiple tests
- Improve code readability by centralizing test data setup
- Example:
JavareuseArbitraryBuilder()
TipsTest.javaProduct discountProduct = PREMIUM_PRODUCT_BUILDER
.set("price", 2000L)
.sample();
Product shippingProduct = PREMIUM_PRODUCT_BUILDER
.set("price", 5000L)
.sample();
then(discountProduct.getCategory()).isEqualTo("PREMIUM");
then(shippingProduct.getCategory()).isEqualTo("PREMIUM");
7. Start with Simple Objects
- Begin with basic objects before moving to complex ones
- Example:
JavastartSimple()
TipsTest.javaProduct product = FIXTURE_MONKEY.giveMeBuilder(Product.class)
.set("name", "Test Product")
.sample();
then(product).isNotNull();
8. Use the IntelliJ Plugin
Install the Fixture Monkey Helper plugin to enhance your development experience:
- Smart code completion for Fixture Monkey methods
- Type-safe field access suggestions using method references
- Quick navigation to field definitions
- Automatic import suggestions for Fixture Monkey classes
- Real-time validation of field names and types
9. Common Use Cases
- Testing validation rules
- Testing business logic with specific conditions
- Creating test data for integration tests
- Generating random but valid test data
10. Best Practices
- Keep test data generation close to where it's used
- Use meaningful variable names
- Document complex test scenarios
- Use constants for frequently used values