我刚刚开始测试。
我尝试测试的方法没有返回值 (void),但它在其自己的类中创建了一个静态二维数组 (char[][]),因此据我了解,这就是它的副作用。
这是一些模拟代码:
public class MyClass{
public static char[][] table;
public void setTable(int rows, int columns, int number){
board = new char[n][m];
// more code that alters the table in a specific way,
// depending on the 3rd parameter
}
现在为了测试,我正在考虑做类似的事情:
public class SetTableTest{
@Test
public void test(){
MyClass test = new MyClass();
assertArrayEquals(**********, test.table);
}
}
我有两个问题:
我是否可以像我那样与静态变量进行比较 (
test.table
) 即。这实际上会返回已完成表格的“实例”吗?我相当确定没有
assertArrayEquals
等价于二维数组,那么我该怎么做呢?
最佳答案
答案已经给出,但为了其他用户,我将添加另一种方法。
要比较两个数组double[][] arr1, arr2
,可以使用返回true
的Arrays.deepequals(arr1, arr2)
code> 或 false
。这个函数的签名是:
java.util.Arrays.deepEquals(Object[] a1, Object[] a2)
对于单元测试,单行就足够了:
assertTrue(Arrays.deepEquals(arr1, arr2));
关于java - Junit 测试 void 二维数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26188423/