Verification by Containment

Let’s consider the code in Listing 7-1.

Listing 7-1: A simple string composition function

public String describeAddition(int left, int right) {  return "The sum of " + left + " and " + right    + " is " + (left + right);}

While the example is trivial and a bit contrived, it is a fair representation of the way strings are commonly handled. How might we test such a method? Well, what do we know about the method? We know some fragments of the text, but those should not change from run to run. What we are probably most concerned with is that the correct values make it into the output. We might write a test like that in Listing 7-2.

Listing 7-2: A test for the code in Listing 7-1

Get Quality Code: Software Testing Principles, Practices, and Patterns now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.