Java 比较 JUnit 断言中的数组,简洁的内置方式?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4228161/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 14:48:11  来源:igfitidea点击:

Comparing arrays in JUnit assertions, concise built-in way?

javaarraysjunitassertions

提问by mBria

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.

是否有一种简洁的内置方法可以对 JUnit 中的两个类似类型的数组进行 equals 断言?默认情况下(至少在 JUnit 4 中)它似乎对数组对象本身进行实例比较。

EG, doesn't work:

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?

..但是有更好的方法吗?

采纳答案by Andy Thomas

Use org.junit.Assert's method assertArrayEquals:

使用org.junit.Assert的方法assertArrayEquals

import org.junit.Assert;
...

Assert.assertArrayEquals( expectedResult, result );

If this method is not available, you may have accidentally imported the Assert class from junit.framework.

如果此方法不可用,您可能不小心从junit.framework.

回答by Bozho

You can use Arrays.equals(..):

您可以使用Arrays.equals(..)

assertTrue(Arrays.equals(expectedResult, result));

回答by Haroldo_OK

I know the question is for JUnit4, but if you happen to be stuck at JUnit3, you could create a short utility function like that:

我知道这个问题是针对 JUnit4 的,但如果你碰巧被 JUnit3 卡住了,你可以创建一个像这样的简短实用函数:

private void assertArrayEquals(Object[] esperado, Object[] real) {
    assertEquals(Arrays.asList(esperado), Arrays.asList(real));     
}

In JUnit3, this is better than directly comparing the arrays, since it will detail exactly which elements are different.

在 JUnit3 中,这比直接比较数组要好,因为它会详细说明哪些元素不同。

回答by csharpfolk

I prefer to convert arrays to strings:

我更喜欢将数组转换为字符串:

Assert.assertEquals(
                Arrays.toString(values),
                Arrays.toString(new int[] { 7, 8, 9, 3 }));

this way I can see clearly where wrong values are. This works effectively only for small sized arrays, but I rarely use arrays with more items than 7 in my unit tests.

这样我就可以清楚地看到错误的值在哪里。这仅适用于小型数组,但我很少在单元测试中使用项目数超过 7 的数组。

This method works for primitive types and for other types when overload of toStringreturns all essential information.

当重载toString返回所有基本信息时,此方法适用于原始类型和其他类型。

回答by winstanr

Using junit4 and Hamcrestyou get a concise method of comparing arrays. It also gives details of where the error is in the failure trace.

使用 junit4 和Hamcrest,您可以获得比较数组的简洁方法。它还提供了错误在故障跟踪中的位置的详细信息。

import static org.junit.Assert.*
import static org.hamcrest.CoreMatchers.*;

//...

assertThat(result, is(new int[] {56, 100, 2000}));

Failure Trace output:

故障跟踪输出:

java.lang.AssertionError: 
   Expected: is [<56>, <100>, <2000>]
   but: was [<55>, <100>, <2000>]