hamcrest containsInAnyOrder only working for specific order

collections, hamcrest, java, junit

Solution

I have discovered what the problem was. It was indeed a version of a hamcrest class that caused the problem.

Steps taken:

- updated the version of the maven-dependency-plugin

- reason:

- Unable to run the command mvn dependency:tree.

- https://cwiki.apache.org/confluence/display/MAVEN/AetherClassNotFound

- changed my mockito dependency from mockito-all to mockito-core

- reason:

- The mockito-all dependency contained a version of the class org.hamcrest.Matcher directly.

- This is not simply visible when you look at what dependencie are loaded (since it's in the mockito-all jar itself and not in a dependency of the jar.

- set the version of hamcrest-core to 1.3 in the dependencyManagement.

- reason:

- mockito-core has a dependency to hamcrest-core but uses version 1.1

- the mockito-core version of hamcrest-core got precedence over the junit version of the hamcrest-core dependency.

So basicaly it was a conflict caused by using mockito-all.

Problem

I'm trying to make a test that checks if an a certain list has items and I don't care about the order. The way I want to be able to do this is by testing the item has a certain property with a certain value. I've isolated the senario with the following code: 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 around the values of the list inside the countainsInAnyOrder, then this works. This was not exactly what I was expecting from the "containsInAnyOrder". What is the correct way to do this? Or is there a way to check that the individual values are present?

Original source