测试这段代码时:
public static int maxRowAbsSum(int[][] array) {
int[][] maxRowValue = {
{3, -1, 4, 0},
{5, 9, -2, 6},
{5, 3, 7, -8}
};
int maxRow = 0;
int indexofMaxRow = 0;
for (int row = 0; row < maxRowValue.length; row++) {
int totalOfRow = 0;
for (int column = 0; column < maxRowValue[row].length; column++){
if (maxRowValue[row][column] > 0) {
totalOfRow += maxRowValue[row][column];
} else {
totalOfRow -= maxRowValue[row][column];
}
}
if (totalOfRow > maxRow) {
maxRow = totalOfRow;
indexofMaxRow = row;
}
}
System.out.println("Row " + indexofMaxRow + " has the sum of " + maxRow);
return indexofMaxRow;
}
使用这个 JUnit 代码:
@Test
public void maxRowAbsSum() {
int [] i = new int [] {};
assertArrayEquals(i, Exercise2.maxRowAbsSum(numArray));
}
这用红色下划线表示 assertArrayEquals:
The method assertArrayEquals(int[], int[]) in the type Assert is not applicable for the arguments (int[], int)
我是不是写错了?我如何使用 JUnit 对其进行测试以使其没有错误或故障?
最佳答案
i 是一个 int 数组,而 Exercise2.maxRowAbsSum(numArray) 返回 int。 比较它们是不可能的,因此是错误的。
关于java - 使用 assertArrayEquals 使用 JUnit 进行测试时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49797311/