
464
Chapter 21 Result Verification Patterns
owing to the number of assertions it may take to specify the expected outcome.
When the same sequence of assertions must be carried out in many tests or many
times within a single Test Method (page 348), we also have Test Code Duplica-
tion (page 213).
Variation: Expected State Specifi cation
When doing Expected State Specifi cation, we construct a specifi cation for the
post-exercise state of the SUT in the form of one or more objects populated with
the expected attributes. We then compare the actual state directly with these
objects using a single call to an Equality Assertion (see Assertion Method). This
tends to result in more concise and readable tests. We can use an Expected State
Specifi cation whenever we need to verify several attributes and it is possible to
construct an object that looks like the object we expect the SUT to return. The
more attributes we have that need to be compared and the more tests that need
to compare them, the more compelling the argument for using an Expected State
Specifi cation. In the most extreme cases, when we have a lot of data to verify, we
can construct an “expected table” and verify that the SUT contains it. Fit’s “row
fi xtures” offer a good way to do this in customer tests; tools such as DbUnit are
a good way to use Back Door Manipulation (page 327) for this purpose.
When constructing the Expected State Specifi cation, we may prefer to use
a Parameterized Creation Method (see Creation Method on page 415) so that
the reader is not distracted by all the necessary but unimportant attributes of
the Expected State Specifi cation. The Expected State Specifi cation is most often
an instance of the same class that we expect to get back from the SUT. We may
have diffi culty using an Expected State Specifi cation if the object doesn’t imple-
ment equality in a way that involves comparing the values of attributes (e.g., by
comparing the object references with each other) or if our test-specifi c defi nition
of equality differs from that implemented by the
equals method.
In these cases, we can still use an Expected State Specifi cation if we create
a Custom Assertion (page 474) that implements test-specifi c equality. Alterna-
tively, we can build the Expected State Specifi cation from a class that imple-
ments our test-specifi c equality. This class can either be a Test-Specifi c Subclass
that overrides the equals method or a simple Data Transfer Object [CJ2EEP] that
implements equals(TheRealObjectClass other). Both of these measures are preferable
to modifying (or introducing) the equals method on the production class, as that
would be a form of Equality Pollution (see Test Logic in Production). When
the class is diffi cult to instantiate, we can defi ne a Fake Object (page 551) that
has the necessary attributes plus an equals method that implements test-specifi c
equality. These last few “tricks” are made possible by the fact that Equality
State
Verifi cation
Also known as:
Expected
Object