Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Show better diff when asserting 'containsAll' in Spock

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?

like image 761
Michal Kordas Avatar asked Feb 06 '23 09:02

Michal Kordas


1 Answers

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

like image 97
OsaSoft Avatar answered Mar 09 '23 00:03

OsaSoft