Hamcrest matcher to compare two arrays

The arrays can be matched with the simplest is matcher, e.g.:

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.core.Is.is;

// ...

assertThat(result, is(new byte[]{1, 2, 3}));

Under the hood it will figure out that the input is an array. It will use the appropriate matcher for arrays (i.e. not just a.equal(b)).


There are many ways that you could do this with hamcrest. The easiest way is to use the arrayContaining matcher in Matchers class.

assertThat(result, Matchers.arrayContaining(expected));