Skip to main content
Version: v1.2.x

Reproducible generation

By default Fixture Monkey generates different values on every run. When a randomly generated fixture reveals a bug, you'll want to reproduce it deterministically. Set a fixed seed on the builder to make generation reproducible.

Fixing the seed on the builder​

FixtureMonkey fixtureMonkey = FixtureMonkey.builder()
.seed(12345L)
.build();

With a fixed seed, the same builder produces the same values — and, as of v1.2.0, the same container sizes — across runs.

What a fixed seed keeps the same​

As of v1.2.6, every decision a sample makes is drawn from a seed scope tied to where it is made, instead of from one stream of random numbers shared by everything. Each builder takes its own scope when it is created, each sample() of the builder takes a scope nested in it, and each node takes a scope nested in the sample's along its path. The value, the container size and the implementation of an interface or abstract type at a node each come from the node's scope.

As a result, with the same seed:

  • Another builder sampling does not change a builder's values. Sampling builder A any number of times before builder B leaves B's values the same, as long as both were created in the same order.
  • Customizing one property does not change the others. Adding set("count", 7) or size("items", 2) changes only that property; the other fields, container sizes and chosen implementations stay as they were.
  • Builders created while a sample is generated stay inside it. A builder created inside setLazy or thenApply takes a scope nested in that sample, so it does not shift what comes after it.
  • The n-th sample() of a builder gives the same value on every run, while consecutive samples still differ from each other.
  • The same holds for Kotlin. Values from Kotest Arbs generated by KotestPlugin, or given with setArb, are drawn from the node's scope as well.
FixtureMonkey fixtureMonkey = FixtureMonkey.builder()
.seed(12345L)
.build();

ArbitraryBuilder<Order> builderA = fixtureMonkey.giveMeBuilder(Order.class);
ArbitraryBuilder<Order> builderB = fixtureMonkey.giveMeBuilder(Order.class);

builderA.sampleList(5); // does not change what builderB generates
List<Order> orders = builderB.sampleList(3);
caution

Only what Fixture Monkey draws is tied to a scope. A value your own code samples — for example calling sample() on a jqwik Arbitrary inside setLazy — comes from the shared random stream: it is still reproducible with the same seed, but it can change when other code samples before it. Pass the Arbitrary to set instead so Fixture Monkey draws it from the node's scope.

// drawn from the node's scope
builder.set("name", Arbitraries.strings().alpha().ofLength(8));

// drawn from the shared stream
builder.setLazy("name", () -> Arbitraries.strings().alpha().ofLength(8).sample());
info

Because decisions are now drawn from scopes, a fixed seed generates different values than it did before v1.2.6. Tests that pinned values produced by a specific seed may need to be re-baselined.

Reproducing a single test with @Seed​

When using the JUnit 5 extension, annotate a test with @Seed to pin its seed. If the test fails, the extension logs the seed that produced the failure so you can reproduce it later.

@Seed(12345L)
@Test
void reproducibleTest() {
// always generates the same values
}
info

As of v1.2.0, an active @Seed takes precedence over FixtureMonkeyBuilder#seed(long): while a @Seed (or FixtureMonkeySeedExtension) is active, the builder's own seed is ignored. The seed is scoped to the test and released afterwards — even if the test or another extension throws — so it no longer leaks into later tests.