Java Reference
In-Depth Information
isGreaterThan(int) . These methods, in turn, also return IntAssert s, allowing many
calls to be chained, as follows:
Assertions.assertThat(x).isGreaterThan(42).isLessThan(108);
You can make this easier to read if you static import Assertions and split the methods
one per line:
assertThat(x).isGreaterThan(42)
.isLessThan(108) ;
All asserts have common methods, like as(String description) , which can be used
to describe the actual object. In the previous example, we could describe it as "X" :
assertThat(x).as("X").isGreaterThan(42)
.isLessThan(108);
And the message would be
java.lang.AssertionError: [X] actual value:<23> should be greater than:<42>
Overall, FEST -Assert assertions are easy to use and straightforward, especially when
using an IDE with autocompletion. But for comparison purposes, here are some
examples of collection assertions, similar to the ones we looked at so far (using
other tools).
Comparing collections contents ( assertThat(LIST1).isEqualTo(LIST4) ):
java.lang.AssertionError: expected:<[4, 8, 15, 16, 108, 23]>
but was:
<[4, 8, 15, 16, 23, 42]>
Checking that a list has a given element ( assertThat(LIST1).contains(666) ):
java.lang.AssertionError: collection:<[4, 8, 15, 16, 23, 42]>
does not contain element(s):
<[666]>
Checking that a list has many given elements ( assertThat(LIST1).con-
tains(108, 666) ):
java.lang.AssertionError: collection:<[4, 8, 15, 16, 23, 42]>
does not contain element(s):
<[108, 666]>
Asserting that a collection doesn't have duplicates ( assertThat(Arrays.
asList(42,42)).doesNotHaveDuplicates() ):
java.lang.AssertionError: collection:<[42,42]>
contains duplicate(s):
[42]>
 
 
 
Search WWH ::




Custom Search