Hamcrest contains InAnyOrder only works for a specific order

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?

+6
source share
1 answer

I found out what the problem is. It really was a version of the hamcrest class that caused the problem.

Steps taken:

  • updated version of maven-dependency-plugin
  • changed my dependence on mockito from mockito-all to mockito-core
    • cause:
      • Depending on mockito-all , a version of the org.hamcrest.Matcher class was contained .
      • This is not just visible when you look at loading dependencies (since it is in the mockito-all bank, and not depending on the flag.
  • set the value of hamcrest-core to 1.3 depending on the control.
    • cause:
      • mockito-core has a dependency on hamcrest-core , but uses version 1.1
      • The mockito-core version of hamcrest-core took precedence over the junit version of hamcrest-core . >.

It was mostly a conflict caused by using mockito-all .

+5
source

Source: https://habr.com/ru/post/972309/


All Articles