这是我的解决方案,它也使用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