Reputation: 219
When testing this bit of code:
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;
}
using this JUnit code:
@Test
public void maxRowAbsSum() {
int [] i = new int [] {};
assertArrayEquals(i, Exercise2.maxRowAbsSum(numArray));
}
This underlines assertArrayEquals in red saying:
The method assertArrayEquals(int[], int[]) in the type Assert is not applicable for the arguments (int[], int)
Am I writing this the wrong way? How do I test this with JUnit so it has no errors or faliures?
Upvotes: 0
Views: 301
Reputation: 219
I fixed my code but still used Karol's example:
Instead of return indexOfMaxRow
which just returned the index of the row that had the max value, I changed this to return maxRow
this returned 23 instead of the 2 that JUnit was expecting.
Upvotes: 1
Reputation: 44952
You are trying to compare an array of int
(int[]
) with a single int
returned from maxRowAbsSum()
method. That's not going to work, it's comparing apples to oranges and JUnit guards you against this with it's method signatures.
You should write your test to match the maxRowAbsSum()
method return type e.g.:
@Test
public void shouldCalculateMaxRowAbsSum() {
int expected = 3; // example value, change to match your test scenario
assertEquals(expected, Exercise2.maxRowAbsSum(numArray));
}
Upvotes: 1
Reputation: 197
i is an int array while Exercise2.maxRowAbsSum(numArray) returns int. Comparing them isn't possible and hence the error.
Upvotes: 3