比较 JUnit 断言中的数组,简洁的内置方式? [英] Comparing arrays in JUnit assertions, concise built-in way?
本文介绍了比较 JUnit 断言中的数组,简洁的内置方式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
是否有一种简洁的内置方法可以对 JUnit 中的两个类似类型的数组进行 equals 断言?默认情况下(至少在 JUnit 4 中)它似乎对数组对象本身进行实例比较.
Is there a concise, built-in way to do equals assertions on two like-typed arrays in JUnit? By default (at least in JUnit 4) it seems to do an instance compare on the array object itself.
EG,不起作用:
int[] expectedResult = new int[] { 116800, 116800 };
int[] result = new GraphixMask().sortedAreas(rectangles);
assertEquals(expectedResult, result);
当然,我可以手动完成:
Of course, I can do it manually with:
assertEquals(expectedResult.length, result.length);
for (int i = 0; i < expectedResult.length; i++)
assertEquals("mismatch at " + i, expectedResult[i], result[i]);
..但是有更好的方法吗?
..but is there a better way?
推荐答案
使用 org.junit.Assert 的方法 assertArrayEquals
:
import org.junit.Assert;
...
Assert.assertArrayEquals( expectedResult, result );
如果此方法不可用,您可能不小心从 junit.framework
导入了 Assert 类.
If this method is not available, you may have accidentally imported the Assert class from junit.framework
.
这篇关于比较 JUnit 断言中的数组,简洁的内置方式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文