这是我的解决方案,它也使用 Apache CommonsArrayUtils#toObject
随着进口
import static org.apache.commons.lang3.ArrayUtils.toObject;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.arrayContaining;
import static org.hamcrest.Matchers.arrayContainingInAnyOrder;
import static org.hamcrest.Matchers.hasItemInArray;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.not;
您可以编写可读的测试,例如
@Test
void primitiveArrayTest() {
int[] actual = new int[]{1, 2, 3};
assertThat(toObject(actual), is(arrayContaining(1, 2, 3)));
assertThat(toObject(actual), is(arrayContainingInAnyOrder(2, 3, 1)));
assertThat(toObject(actual), hasItemInArray(2));
assertThat(toObject(actual), is(not(arrayContaining(-5))));
}
is
只是用来提高可读性。