I need to perform assertion that a collection contains all elements from the other collection.
The following test should fail, because first collection doesn't contain 7
from the second one:
def first = [6, 1, 5, 2, 4, 3]
def second = [3, 4, 2, 5, 7, 6]
expect:
first.containsAll(second)
However, test failure is not readable at all. It is not clear that just 7
is missing:
left.containsAll(right)
| | |
| false [3, 4, 2, 5, 7, 6]
[6, 1, 5, 2, 4, 3]
AssertJ deals with that much better:
java.lang.AssertionError:
Expecting:
<[6, 1, 5, 2, 4, 3]>
to contain:
<[3, 4, 2, 5, 7, 6]>
but could not find:
<[7]>
What kind of assertion would be idiomatic in Spock to get better failure messages for containsAll
?
I guess you could hack around and do something like (right - left).isEmpty()
which should print out the elements that are in right but not in left.
It is a bit of a hacky way, but really anything I can come up with
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With