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?
I have discovered what the problem was. It was indeed a version of a hamcrest class that caused the problem.
Steps taken:
So basicaly it was a conflict caused by using mockito-all.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With