liveBook · Manning

Unit test

As explained in Data-Oriented Programming in Java

A unit test is an automated check that evaluates a relatively small, isolated unit of software behavior.

A unit test is an automated check that runs in isolation from external dependencies. Its unit may be a method, several methods, or an entire feature; the defining property is isolation, with dependencies controlled through mocks. section 11.1

Isolation makes deep and exhaustive checks practical across a wide state space, but it also limits the test to approximating the outside world. Integration tests complement it by exercising dependencies and input/output. section 11.6

Effective unit tests make their data and intent visible:

  • Keep relevant inputs, transformations, and assertions together so the link between data and expected behavior is clear. section 11.8
  • Parameterize fixtures and generate varied values to expose rarely exercised states and avoid secret couplings created by hard-coded data. section 11.8
  • Choose the strategy for the application and problem rather than treating a testing pyramid or coverage target as a universal rule. section 11.6

One test, several scales

A test can generate ten accounts for a quick isolated check, then use the same assertions with 1,000 or 1,000,000 accounts to investigate load behavior. Changing the data volume changes the role without changing the test’s core logic. section 11.5.3

Listing 11.51 The same test can fill multiple roles just by changing the amount of data
Set<Account> accounts = generate(this::mkAcnt)       
        .limit(10)                            #A
        // .limit(1000)                       #A
        // .limit(1_000_000)                  #A
        .collect(toSet());
// rest of test is the same 

Compare the same generated test at small, medium, and million-item data volumes. section 11.5.3

How Data-Oriented Programming in Java covers it

How deep it goes
Defines isolation, mocks, strengths, and limitations, then defers dependency behavior to integration tests.
Examples
Demonstrates generated account sets at ten, 1,000, and 1,000,000 items while retaining the same test.
What it assumes
Assumes software behavior to evaluate and distinguishes controlled dependencies from external input/output.
What you can do after
Evaluate isolated behavior deeply, vary test states, and recognize when integration testing is needed.
How it connects
Connects unit tests with integration tests, mocks, generated test data, randomization, load testing, and data-oriented testing.

Also explained in

Compare how 18 books explain unit test →

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.