hamcrest: how to match array is subset of another array?

Create your own custom matcher by extending org.hamcrest.TypeSafeMatcher and use it in the assertThat() method. You can refer the code of org.hamcrest.collection.IsArrayContaining and create your own matcher


You can use a combination of the Every and IsIn matcher:

assertThat(Arrays.asList(a), everyItem(in(b)));

This does check if every item of a is contained in b. Make sure a and b are of type Integer[] otherwise you might get unexpected results.

If you are using an older version of hamcrest (for example 1.3) you can use the following:

assertThat(Arrays.asList(a), everyItem(isIn(b)));

In the latest version isIn is deprecated in favor of in.