Java Reference
In-Depth Information
[5] => 42
[5] => 23
This message might look cryptic at first sight, but it's pretty straightforward: it says that
elements on indexes 4 and 5 are different (23 instead of 108, and 42 instead of 23,
respectively). If the order of the elements isn't important, you could pass
LENIENT_ORDER as parameter or call assertLenientEquals() instead. The message in
this case would be slightly different, though, as shown here:
junit.framework.AssertionFailedError: Found following differences:
[5,4]
=> 42
=> 108
--- Difference details ---
=> [4, 8, 15, 16, 23, 42]
=> [4, 8, 15, 16, 108, 23]
[5,4] => 42
[5,4] => 108
Here it shows that these lists have only one element that's different, although they're
different in distinct locations (indexes 5 and 4, respectively).
Overall, ReflectionAssert is a powerful class. Once you break the learning curve
barrier, you get a valuable tool for your day-to-day assertions.
19.4.3
FEST Fluent Assertions Module
FEST Fluent Assertions Module (also called FEST -Assert) provides custom assertion
for many types of objects, such as collections, strings, exceptions, files, Big-
Decimals , and even BufferedImages . And not only does it supports a great variety
of objects, but the assertions are expressed in a different syntax, similar to the
Hamcrest syntax (described in chapter 3) but even more natural, because the asser-
tion methods can be chained.
The entry point for the assertions is the method assertThat(actual) from the
org.fest.assertions.Assertions class. That method is overloaded a dozen times,
each with a different type for the actual parameter and returning the proper asser-
tion class for that type. Sound confusing? It's quite clever and simple (once you get
used to it), so let's use the x > 42 example to make it clear. Assuming a variable x of
type int, that assertion would be
Assertions.assertThat(x).isGreaterThan(42);
And the result would be
java.lang.AssertionError: actual value:<23> should be greater than:<42>
Behind the scenes, all Assertions.assertThat() methods return a subclass of
Assert , in this case an IntAssert (because x is an int), which has methods such as
 
 
 
 
 
 
 
 
 
 
 
Search WWH ::




Custom Search