JavaHamcrest
                                
                                
                                
                                    JavaHamcrest copied to clipboard
                            
                            
                            
                        containsInAnyOrder can depend on order
The algorithm goes like: for each item, try each matcher in the given order and consume the first one that matches.
This causes the following test to fail:
    @Test
    public void containsInAnyOrderFailsToTryAllCombinations() {
        assertThat(
                Arrays.asList("foo", "friend"),
                containsInAnyOrder(
                        containsString("f"),
                        containsString("o")
                )
        );
    }
Whereas inverting the two containsString lines makes it pass.
The doc says:
N.B. each of the specified matchers will only be used once during a given examination, so be careful when specifying matchers that may be satisfied by more than one entry in an examined iterable
So that could explain it, but it's still surprising for the user.
This seems like greedy consumption of elements. It could be addressed by backtracking, but I'm not sure what the performance of that would be like, and it would certainly break that "used only once" contract
This is a good point. I expect someone out there has a clever algorithmic solution...
I don't know if it is clever, but there is a pull request with some (rather naive) algorithm.