liveBook · Manning

Randomization

As explained in Data-Oriented Programming in Java

Randomization is the generation or selection of varying test inputs to exercise a broader range of program states than fixed examples.

Randomization replaces a small, fixed set of test inputs with varying data. Its purpose is not to claim exhaustive proof, but to exercise program states that hand-picked examples are unlikely to reach. A randomized threshold, account, collection size, or nested collection broadens what an assertion says: not merely “this example works,” but “this rule should hold across many generated states.” section 11.5.2 section 11.8

Useful generators are guided rather than indiscriminate. Construct data that deliberately includes the states needed for an invariant—such as accounts above, at, and below a threshold—then vary the attributes around those states. Alternatively, generate a broad population and derive the relevant subset with filters and grouping. Combining specialized generators supports larger “for all” assertions while retaining meaningful cases. section 11.5.1 section 11.5.2

Listing 11.40 Generating collections of specialized randomized data
@Test
void specializingTheGeneratedData() {
    List<Account> emeaAccounts =                      #A
        generate(() -> mkAcnt().withRegion(EMEA))     #A
        .limit(100)                                   #A
        .toList();                                    #A
    List<Account> accountStates =                                     #B
        generate(() -> mkAcnt().withId(new AccountId("000000001")))   #B
        .limit(1000)                                                  #B
        .toList();                                                    #B
    // and then feed them into your test           
}

Specialized generators show how to preserve the property under test, such as a region or account identity, while randomizing the remaining account data. section 11.5.1

Randomization can vary more than individual field values:

  • Generate streams of records and limit them to the amount a test needs. section 11.5.1
  • Randomize collection sizes, including zero, so empty-input behavior is exercised without a separate remembered edge case. section 11.5.1
  • Nest generators to produce collections of collections and exercise combinations from multiple angles. section 11.5.1
  • Combine focused streams, or generate broadly and carve out subsets, to build assertions over mixed state populations. section 11.5.1

Round-trip boundaries with randomized accounts

A compact boundary test generates many accounts, serializes each one, deserializes it, and asserts equality with the original. Repeating this across randomized identifiers, regions, values, and other fields can expose serialization or dependency defects involving precision, input size, empty values, malformed values, or Unicode. section 11.5.4

Randomization trades away determinism. When a generated case fails, reproducing it requires recording and replaying the seed; choosing distributions and ensuring edge cases also become design decisions. Thousands of generated cases can be difficult to debug, so hand-built generation is a useful starting point, while intermediate practice generally benefits from libraries. section 11.5.7 section 11.5.6

Do not randomize merely because more data is available. When a domain has a small, fully enumerated set of meaningful states, explicit examples may be clearer, deterministic, and already exhaustive. Randomization earns its complexity when it expands the states an assertion can genuinely examine; coverage alone is not quality. section 11.5.6 section 11.8

In integration tests, randomization can name queues, buckets, and other on-demand resources uniquely for each test. Unique namespaces isolate a run from stale resources left by earlier failures. Pair random suffixes with structured owner, suite, and test prefixes so cleanup can target one test, a suite, or all owned resources. section 12.6

Listing 12.14 Randomizing the infrastructure
@Test
void myCoolIntegrationTest () {
  // spinning up infrastructure right before we run the test
  Queue inputQueue = cloudSDK.createQueue(UUID.randomUUID().toString());
  Queue outputQueue = cloudSDK.createQueue(UUID.randomUUID().toString()); 
  Bucket bucket = cloudSDK.createBucket(UUID.randomUUID().toString());

  // rest of test 
}

Random UUID-based infrastructure names illustrate isolation: each test provisions distinct queues and buckets instead of sharing names from prior runs. section 12.6

How Data-Oriented Programming in Java covers it

How deep it goes
Demonstrates Stream-based generators, constrained state construction, nested collections, round-trips, and operational tradeoffs; defers library-specific implementation.
Examples
Uses randomized accounts, thresholds, collection sizes, serialization round-trips, and UUID-named queues and buckets.
What it assumes
Assumes Java testing and Stream use while building random-data generation patterns from simple supplier functions.
What you can do after
Apply it via Java Streams to generate guided test data, probe edge states, and isolate integration-test infrastructure.
How it connects
Connects randomized testing to Stream.generate, test invariants, property-based testing, serialization, integration testing, and dependency injection.

Also explained in

Compare how 7 books explain randomization →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Data-Oriented Programming in Java. The section links point to the parts of the book it draws on.