liveBook · Manning

Integration test

As explained in Data-Oriented Programming in Java

Integration testing evaluates interactions between multiple software components or external dependencies.

An integration test checks behavior where code meets real dependencies: databases, queues, files, networks, or other services. It is defined here by permission to perform I/O rather than by size; a tiny database read/write check and a system-wide flow can both qualify. The useful distinction is that unit tests run in isolation with mocked dependencies, while integration tests verify what actually happens beyond that boundary. section 11.1 section 12.1

Start with a concrete interaction and make its initial conditions, inputs, and expected effects visible. Many tests fit arrange, act, assert: create isolated infrastructure, put the system in a known state, run it, then inspect results. Others need an assert-as-you-go shape, alternating actions and checks as asynchronous or multi-step behavior unfolds. section 12.1 section 12.8

Verify a repository against the database

A basic test constructs a real repository, saves a Person with a generated identifier, then loads it and asserts equality. The point is not repository logic in isolation; it is evidence that the repository and database can exchange the domain value as expected. section 12.1

Drive the test with generated domain data instead of opaque fixture files. Build rows in the test, derive the expected subset from those same rows, serialize the input, run the service, and compare the resulting identifiers. This exposes the causal path from input to assertion, avoids mysterious hard-coded provenance, and makes scaling from hundreds to millions of records a parameter change rather than a redesign. section 12.2 section 11.5.3

Listing 12.4 Driving the test with data
@Test
void extractsReportDataAndWritesToDB() throws IOException {
  // [REMOVED] Path path = Path.of("path/to/report.csv");  #A
 
  Set<Row> rows = Stream.generate(this::randRow) #B
    .limit(100)                                  #B
    .collect(Collectors.toSet());                #B  
 
 
  Set<Row> usaRelated = rows.stream()                #C
    .filter(x -> x.country().equals(CountryCode.US)) #C
    .collect(Collectors.toSet());                    #C 
 
  String fileContents = someCsvLibrary.serialize(rows); #D
 
  reportsRepo.upload("report.csv", fileContents);
  reportsQueue.put("report.csv");
  myService.run();
  
  Set<Account> results = myService.listAccounts();
 
  Set<AccountId> ingested =                              #E
    results.stream().map(Account::id).collect(toSet());  #E
  Set<AccountId> expected =                              #E 
    usaRelated.stream().map(Row::id).collect(toSet());   #E
  assertEquals(expected, ingested);                      #E  
}
 
// ------------ Supporting Records and Methods -------
record Row(
  AccountId id,
  Region region,
  CountryCode country
  // etc...
){}
 
Random rand = new Random(); 
Row randRow() {
  return new Row(
    new AccountId(UUID.randomUUID()),
    Region.values()[rand.nextInt(Region.values().length)],
    CountryCode.values()[rand.nextInt(CountryCode.values().length)]
    // and so on...
  );
}

This listing makes the test data, the expected US-related rows, serialized CSV input, and resulting account identifiers explicit in one flow. section 12.2

Use integration tests where a dependency must enforce rules that application code cannot safely guarantee. Model the allowed states or transitions, derive a simple law such as an ordering, then exercise every relevant pair against the real database and assert whether each write is accepted or rejected. Mocks can preserve assumptions; this checks the foreign system's actual constraint expression. section 12.3 section 12.8

Keep a suite trustworthy despite real-world failures:

  • Give every test isolated infrastructure so it cannot observe another test's data, delays, or failed cleanup. section 12.6
  • Provision queues, buckets, and similar dependencies on demand; randomize or namespace their names and support scoped cleanup. section 12.6
  • Prune tests that larger, higher-level flows now cover, because redundant integration tests cost time and add flakiness. section 12.5
  • Tailor the mix and shape of tests to the application rather than treating a testing pyramid or coverage count as a universal rule. section 11.6 section 11.8

Integration testing is difficult precisely because it reaches systems you do not fully control: unreliable networks, outages, eventual consistency, corrupted data, races, and throughput limits. Its reward is confidence about these edges, where isolated tests cannot reach. Treat the suite as production-quality code: model its data, expose its intent, experiment with concurrency and randomization, and keep only tests that earn their maintenance cost. section 12.0 section 12.7

How Data-Oriented Programming in Java covers it

How deep it goes
Develops data-driven designs, algebraic dependency checks, isolation, provisioning, cleanup, and test-pruning practices.
Examples
Uses database repository reads and writes, CSV ingestion, task-state transitions, queues, and buckets.
What it assumes
Assumes familiarity with unit tests and mocking while defining integration tests through I/O and real dependencies.
What you can do after
Build from scratch reliable tests that exercise real dependencies, expose data flow, and isolate infrastructure.
How it connects
Connects unit tests, mocks, dependency injection, state machines, ordering, randomization, and eventual consistency.

Also explained in

Compare how 40 books explain integration 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.