I am new to java and I am learning about JUnit testing, All examples I find online are about adding two number.
This is my method on class SumOfAllSeries
static int getIndex(int[] array) {
int max = 0;
for(int i= 0; i < array.length; i++) {
if(array[i] > array[max]) {
max = i;
}
}
return max;
}
This is what I tried to do on my JUnit and I cant seem to find it right, Please how can I test this code.I have an error: The method assertArrayEquals(int[], int[]) in the type Assert is not applicable for the arguments (int[], int) when I use AssertEquals it prints garbage and says expected is 4.
class SumOfAllSeriesTest {
@Test
void testGetIndex() {
int array[] = {2,4,5,6,7};
int calculateIndex = SumOfAllSeries.getIndex(array);
assertEquals(4, calculateIndex);
}
}
Update: I have tried to change array to 4 and used
assertEquals
Is this the correct way? I don't have anyone to ask and i want to understand this concept.
int[]as parameters, you give it anint[]and anint. What should it even mean for anint[]to be equal to anint?resulttocalculatedIndex. Does that make more sense when reading?