You can use Spock's Hamcrest support and use Matcher explicitly for this case - containsInAnyOrder . You need the following imports:
import static org.hamcrest.Matchers.containsInAnyOrder import static spock.util.matcher.HamcrestSupport.that
Then you can write your test code as follows:
given: def companyList = [ "Second", "First"] expect: that companyList, containsInAnyOrder("First", "Second")
This has the advantage over using .sort() in that duplicate items in the List are considered correctly. The following test will not work using Hamcrest, but will pass using .sort()
given: def companyList = [ "Second", "First", "Second"] expect: that companyList, containsInAnyOrder("First", "Second") Condition not satisfied: that companyList, containsInAnyOrder("First", "Second") | | | [Second, First, Second] false Expected: iterable over ["First", "Second"] in any order but: Not matched: "Second"
If you use then: instead of expect: you can use expect instead of that to read to read.
then: expect companyList, containsInAnyOrder("First", "Second")
source share