在JUnit中是否有一种简洁的、内置的方法来对两个相似类型的数组执行等于断言?默认情况下(至少在JUnit 4中),它似乎对数组对象本身进行实例比较。
EG,不起作用:
int[] expectedResult = new int[] { 116800, 116800 };
int[] result = new GraphixMask().sortedAreas(rectangles);
assertEquals(expectedResult, result);
当然,我可以手动使用:
assertEquals(expectedResult.length, result.length);
for (int i = 0; i < expectedResult.length; i++)
assertEquals("mismatch at " + i, expectedResult[i], result[i]);
..但是有没有更好的办法呢?
使用junit4和Hamcrest可以得到一个比较数组的简洁方法。它还提供了错误在故障跟踪中的位置的详细信息。
import static org.junit.Assert.*
import static org.hamcrest.CoreMatchers.*;
//...
assertThat(result, is(new int[] {56, 100, 2000}));
故障跟踪输出:
java.lang.AssertionError:
Expected: is [<56>, <100>, <2000>]
but: was [<55>, <100>, <2000>]
使用junit4和Hamcrest可以得到一个比较数组的简洁方法。它还提供了错误在故障跟踪中的位置的详细信息。
import static org.junit.Assert.*
import static org.hamcrest.CoreMatchers.*;
//...
assertThat(result, is(new int[] {56, 100, 2000}));
故障跟踪输出:
java.lang.AssertionError:
Expected: is [<56>, <100>, <2000>]
but: was [<55>, <100>, <2000>]