Right-BIC[E]P: Forcing Error Conditions

The existence of a happy path suggests that there must be an unhappy path. Errors happen, even when you think they can’t possibly. Disks fill up, network lines drop, email goes into a black hole, and programs crash. You want to test that your code handles all of these real-world problems in a graceful or reasonable manner. To do so, you need to write tests that force errors to occur.

That’s easy enough to do with invalid parameters and the like, but to simulate specific network errors-—without unplugging any cables—-takes some special techniques. We’ll discuss one way to do this in here.

First, however, think about what kinds of errors or other environmental constraints you might introduce to test your ...

Get Pragmatic Unit Testing in Java 8 with JUnit now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.