liveBook · Manning

Test isolation

As explained in Data-Oriented Programming in Java

Isolation is execution of a test without interaction with its external or substituted dependencies.

Test isolation runs each test without interaction with other tests, shared infrastructure, or leftover state. It makes failures trustworthy despite unreliable networks and eventual consistency. section 12.6

Build isolation into integration tests by:

  • Provisioning queues, buckets, and other infrastructure on demand for each test. section 12.6
  • Injecting those resources into the service under test through dependency injection. section 12.6
  • Using randomized names and namespaces to prevent collisions with prior failures or concurrent tests. section 12.6
  • Cleaning resources by ownership, suite, or test scope. section 12.6

An isolated integration test

Create private input and output queues plus a bucket, construct the service with those dependencies, enqueue an input message, run the service, and assert the result. section 12.6

Listing 12.13 Creating isolated dependencies for a test
@Test
void myCoolIntegrationTest () {
  // spinning up infrastructure right before we run the test
  Queue inputQueue = cloudSDK.createQueue("my-own-input-queue");
  Queue outputQueue = cloudSDK.createQueue("my-own-output-queue"); 
  Bucket bucket = cloudSDK.createBucket("bucket-for-my-test"); 

  // We construct our service using the placeholder infrastructure. 
  // (This, of course, depends on us using good Dependency 
  //  Injection practices)
  MyService service = new MyService(inputQueue, outputQueue, bucket);
  // now it’s pretty standard arrange, act, assert.
  // just that the “arrange” is done against service infrastructure 
  // that’s completely isolated to our test
  inputQueue.put(makeInputMessage());
  
  service.run()

  // then we make sure everything behaved the way we expect. 
  // for instance, the item in the input queue should have been consumed
  assertEquals(inputQueue.poll(), Optional.empty());
  // we want something in the output queue 
  assertNotEquals(outputQueue.poll(), Optional.empty());
  // and something written to our bucket 
  assertEquals(/* lookup item in bucket here */); 

  // then blow everything away 
  cloudSDK.deleteQueue(inputQueue.name());
  cloudSDK.deleteQueue(outputQueue.name());
  cloudSDK.deleteBucket(bucket.name());
}

Shows the complete arrange–act–assert flow using infrastructure created immediately before the test. section 12.6

How Data-Oriented Programming in Java covers it

How deep it goes
Explains provisioning, injection, naming, and cleanup, while noting that isolation becomes harder with eventually consistent stores.
Examples
Uses queues, buckets, randomized resource names, namespaces, and scoped cleanup as concrete isolation mechanisms.
What it assumes
Assumes integration-test and dependency-injection knowledge while treating infrastructure lifecycle and cleanup as explicit design concerns.
What you can do after
Build reliable integration tests with private dependencies and cleanup scopes that survive failures in earlier tests.
How it connects
Connects integration testing with dependency injection, cloud infrastructure, ACID databases, and eventual consistency.

Also explained in

Compare how 6 books explain test isolation →

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.