4

There's a StringContainsInOrder Matcher in Hamcrest.

How can I assert that a String contains a collection of Strings in any order?

brabec
  • 4,632
  • 8
  • 37
  • 63

2 Answers2

4

You can combine multiple contains matcher.

assertThat("this is a string", allOf(
  contains("string"),
  contains("this"),
  contains("a")));
Stefan Birkner
  • 24,059
  • 12
  • 57
  • 72
0

It's not possible, because of the way how Hamcrest checks the collection.

It is iterating over it, look here: https://code.google.com/p/hamcrest/source/browse/trunk/hamcrest-java/hamcrest-library/src/main/java/org/hamcrest/text/StringContainsInOrder.java?r=375

You could:

1) test each order if it is short String (not effective)

2) sort and test few times, believing that it will cover more than 90% cases (not adequate)

Hope it helps.

m.aibin
  • 3,528
  • 4
  • 28
  • 47