I am trying to do a test that checks to see if there are any items in a specific list, and I don't care about the order.
The way I want to do this is to verify that the element has a specific property with a specific value.
I isolated senario with the following code:
The class I'm using:
public class A { private String propA; public A (final String propA) { this.propA = propA; } public String getPropA() { return propA; } public void setPropA(final String propA) { this.propA = propA; } }
Testclass
import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.beans.HasPropertyWithValue.hasProperty; import static org.hamcrest.collection.IsIterableContainingInAnyOrder.containsInAnyOrder; import java.util.ArrayList; import java.util.List; import org.junit.Test; public class HamcrestCollectionTest { @Test public void testContainsInAnyOrder() { List<A> list = new ArrayList<A>(); list.add(new A("a")); list.add(new A("b")); assertThat(list, containsInAnyOrder(hasProperty("propA", equalTo("b")), hasProperty("propA", equalTo("a")))); } }
This test fails. If I switch the list values ββinside countainsInAnyOrder, then this works. This is not exactly what I expected from "containsInAnyOrder".
What is the right way to do this?
Or is there a way to check for individual values?
source share