Java Reference
In-Depth Information
Figure 3.1 The screen on the left shows the stack trace from the execution of the test without using
Hamcrest, and the one on the right shows the same thing using Hamcrest.
with or without Hamcrest matchers is a personal preference. What Hamcrest gives
you that standard assertions don't provide is a human-readable description of an
assertion failure.
If you followed the examples in the two previous listings, you've probably noticed
that in both the cases we construct a List with the “x”, “y”, and “z” as elements in it.
After that we assert the presence of either “one”, “two”, or “three”, which means that
the test, as written, will fail. Let's execute that test. The result from the execution is
shown in figure 3.1.
As you can see from the two screens, the one on the right gives a lot more details,
doesn't it? Table 3.2 lists some of the most commonly used Hamcrest matchers.
Table 3.2
Some of the most commonly used Hamcrest matchers
Core
Logical
anything
Matches absolutely anything. Useful in some
cases where you want to make the assert state-
ment more readable.
is
Is used only to improve the readability of your
statements.
allOf
Checks to see if all contained matchers match
(just like the && operator).
Checks to see if any of the contained matchers
match (like the || operator).
anyOf
Traverses the meaning of the contained matchers
(just like the ! operator in Java).
not
instanceOf, isCompatibleType
Match whether objects are of compatible type
(are instances of one another).
 
 
 
 
 
 
 
 
 
Search WWH ::




Custom Search