Search code examples
javatestingjunitjunit4

Error when testing with JUnit using assertArrayEquals


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?


Solution

  • 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.