Error Injection

We can use injection to introduce the data of our choice for testing our software but we can also use it to force error conditions. Although best practice suggests that we keep our methods as small and as simple as possible, there are many times when the tradeoff against design complexity leads to a little more complexity in a method. Consider the hypothetical code to retrieve a response with a proprietary network protocol shown in Listing 8-6.

Listing 8-6: Typical management of connection-oriented protocol in Java

public class NetRetriever {  public NetRetriever() {  }  public Response retrieveResponseFor(Request request)      throws RetrievalException {    try {      openConnection();      return ...

Get Quality Code: Software Testing Principles, Practices, and Patterns 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.