FixtureMonkey

To generate test fixtures, the first step is to create a FixtureMonkey instance, which facilitates the creation of test fixtures.

You can use the create() method to generate a FixtureMonkey instance with default options. For Kotlin environments, add the Kotlin plugin.

FixtureMonkey fixtureMonkey = FixtureMonkey.create();
val fixtureMonkey = FixtureMonkey
  .plugin(KotlinPlugin())
  .build()

If you want to add some options for creating or customizing the test fixtures, you can add them using the FixtureMonkey builder.

FixtureMonkey fixtureMonkey = FixtureMonkey.builder()
    + options...
    .build();
val fixtureMonkey = FixtureMonkey.builder()
    + options...
    .build()

For information on what options are available, see the Fixture Monkey Options section.

Generating instances

The FixtureMonkey class provides several methods to help create test objects of the required type.

giveMeOne()

If you need an instance of a certain type, you can use giveMeOne(). Pass either a class or a type reference.

Product product = fixtureMonkey.giveMeOne(Product.class);

List<String> strList = fixtureMonkey.giveMeOne(new TypeReference<List<String>>() {});
val product: Product = fixtureMonkey.giveMeOne()

val strList: List<String> = fixtureMonkey.giveMeOne()

giveMe()

If you need multiple instances of a certain type, you can use the giveMe() method. You can choose to generate either a stream of instances or a list by specifying the desired size.

Stream<Product> productStream = fixtureMonkey.giveMe(Product.class);

Stream<List<String>> strListStream = fixtureMonkey.giveMe(new TypeReference<List<String>>() {});

List<Product> productList = fixtureMonkey.giveMe(Product.class, 3);

List<List<String>> strListList = fixtureMonkey.giveMe(new TypeReference<List<String>>() {}, 3);
val productSequence: Sequence<Product> = fixtureMonkey.giveMe()

val strListSequence: Sequence<List<String>> = fixtureMonkey.giveMe()

val productList: List<Product> = fixtureMonkey.giveMe(3)

val strListList: List<List<String>> = fixtureMonkey.giveMe(3)

giveMeBuilder()

If you need to further customize the instance to be created, you can use giveMeBuilder(). This will return an ArbitraryBuilder of the given type. An ArbitraryBuilder is a class in Fixture Monkey that acts as a builder for an Arbitrary object of the given class.

ArbitraryBuilder<Product> productBuilder = fixtureMonkey.giveMeBuilder(Product.class);

ArbitraryBuilder<List<String>> strListBuilder = fixtureMonkey.giveMeBuilder(new TypeReference<List<String>>() {});
val productBuilder: ArbitraryBuilder<Product> = fixtureMonkey.giveMeBuilder()

val strListBuilder: ArbitraryBuilder<List<String>> = fixtureMonkey.giveMeBuilder()

For cases where you already have a generated instance and want to customize it further, you can also use giveMeBuilder().

Product product = new Product(1L, "Book", ...);

ArbitraryBuilder<Product> productBuilder = fixtureMonkey.giveMeBuilder(product);
val product = Product(1L, "Book", ...)

val productBuilder = fixtureMonkey.giveMeBuilder(product)

The generated ArbitraryBuilder can be used for further customization of your fixture. For more information on customization options, see the section on customization objects.

To obtain an instance from the ArbitraryBuilder, you can use the sample(), sampleList(), sampleStream() methods of the ArbitraryBuilder.

ArbitraryBuilder<Product> productBuilder = fixtureMonkey.giveMeBuilder(Product.class);

Product product = productBuilder.sample();
List<Product> productList = productBuilder.sampleList(3);
Stream<Product> productStream = productBuilder.sampleStream();
val productBuilder: ArbitraryBuilder<Product> = fixtureMonkey.giveMeBuilder()

val product = productBuilder.sample()
val productList = productBuilder.sampleList(3)
val productStream = productBuilder.sampleStream()

In cases where you need an Arbitrary itself rather than an instance, you can simply call the build() method.

ArbitraryBuilder<Product> productBuilder = fixtureMonkey.giveMeBuilder(Product.class);

Arbitrary<Product> productArbitrary = productBuilder.build();
val productBuilder: ArbitraryBuilder<Product> = fixtureMonkey.giveMeBuilder()

val productArbitrary = productBuilder.build()

giveMeArbitrary()

To get an Arbitrary of the specified type, you can use the giveMeArbitrary() method.

Arbitrary<Product> productArbitrary = fixtureMonkey.giveMeArbitrary(Product.class);

Arbitrary<List<String>> strListArbitrary = fixtureMonkey.giveMeArbitrary(new TypeReference<List<String>>() {});
val productArbitrary: Arbitrary<Product> = fixtureMonkey.giveMeArbitrary()

val strListArbitrary: Arbitrary<List<String>> = fixtureMonkey.giveMeArbitrary()