Search code examples
javajunithamcrest

Hamcrest containsInAnyOrder match if string contains


Let's say I have

@Test
public void iterable_matches_order () {

    List<String> cloths = Lists.newArrayList("shirts", "shoes", "pants", "socks");

    assertThat(cloths, containsInAnyOrder("shi", "sho", "pan", "soc"));
}

I want this to pass because shi is contained in shirts, sho is contained in shoes and so on. containsInAnyOrder seems to pass only when each entry is an exact match.


Solution

  • According to documentation: http://hamcrest.org/JavaHamcrest/javadoc/1.3/org/hamcrest/Matchers.html#containsInAnyOrder(T...) it doesn't match substring, but rather whole string

    assertThat(Arrays.asList("foo", "bar"), containsInAnyOrder("bar", "foo"))