在JUnit断言中比较数组,简洁的内置方式? [英] Comparing arrays in JUnit assertions, concise built-in way?

查看:113
本文介绍了在JUnit断言中比较数组,简洁的内置方式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有简洁的内置方法在JUnit中对两个类似类型的数组进行等于断言?默认情况下(至少在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 。

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

这篇关于在JUnit断言中比较数组,简洁的内置方式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆