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





