brabec
brabec

Reputation: 4730

stringContainsInAnyOrder matcher in Hamcrest?

There's a StringContainsInOrder Matcher in Hamcrest.

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

Upvotes: 4

Views: 611

Answers (2)

Stefan Birkner
Stefan Birkner

Reputation: 24510

You can combine multiple contains matcher.

assertThat("this is a string", allOf(
  contains("string"),
  contains("this"),
  contains("a")));

Upvotes: 4

m.aibin
m.aibin

Reputation: 3593

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.

Upvotes: 0

Related Questions