I need to test method which returns ordered List of some complex objects. Simplified example:
class MyObject {
public String foo() { return someString; }
}
I want to test both: orderable of returned collection (since now I was using org.hamcrest.collection.IsIterableContainingInOrder.contains and fulfiling predicate).
To sum up. I’m looking for such syntax:
@Test
public void shouldMatchPredicate() {
List<MyObject> collection = testObject.generate();
//collection = [myObject#x, myObject#y, myObject#z]
assertThat(collection, somePredicate("x", "y", "z")
}
Default one, contains method is not working, since first argument is Collection<MyObject> and arguments in predicate are Strings. I need some kind of bridge between it.
Since
Predicateis a Guava object and Hamcrest does not depend on Guava it will not have a Matcher that will take aPredicate. Also, since Guava is not dependent on Hamcrest, they will not provide a Matcher either.I suggest writing your own Matcher that takes a Predicate. This is relatively easy to do. Get the source code for
IsIterableContainingInOrderand modify it to take aPredicate.Another option would be to do the following:
This won’t give you much documentation on a failure but it will pass/fail properly.