I’m trying to write a test for the following method:
public class Sort {
...
...
...
public static String[][] findRanks(String[][] array, int indexOfPoints, int indexOfRank) {
for (int i = 0; i < array.length - 1; i++) {
int compare = 1;
if (i < array.length - 2)
compare = Double.valueOf(array[i][indexOfPoints]).compareTo(Double.valueOf(array[i + 1][indexOfPoints]));
if (i == array.length - 1 || compare != 0) {
array[i][indexOfRank] = Integer.toString(i + 1);
}
else {
array[i][indexOfRank] = Integer.toString(i + 1) + " - " + Integer.toString(i + 2);
array[i+1][indexOfRank] = Integer.toString(i + 1) + " - " + Integer.toString(i + 2);
i++;
}
}
return array;
}
}
I’ve tryed the following test:
import static org.junit.Assert.*;
import junit.framework.TestCase;
import org.junit.Test;
public class SortTest extends TestCase {
@Test
public void testFindRanks() {
String[][] array = { {"Siim Susi","12.61","5.00","9.22","1.50","60.39","16.43","21.60","2.60","35.81","5.25.72","6253.0","1"},
{"Beata Kana","13.04","4.53","7.79","1.55","64.72","18.74","24.20","2.40","28.20","6.50.76","5290.0","2"}};
Sort test1 = new Sort(array, 11, 12); //This is where the problem is
String[][] expected = { {"Siim Susi","12.61","5.00","9.22","1.50","60.39","16.43","21.60","2.60","35.81","5.25.72","6253.0","1"},
{"Beata Kana","13.04","4.53","7.79","1.55","64.72","18.74","24.20","2.40","28.20","6.50.76","5290.0","2"}};
assertTrue(expected.equals(test1));
fail("Not yet implemented");
}
}
But the test keeps telling me “the constructor Sort(String[][], 11, 12);” is undefined.
Why does it think it has to be a constructor and how do i fix this?
Thank you.
You should write
and to make your unit tests cleaner, more idiomatic and easier to maintain, you could also refactor it a bit:
That is, separate the test setup, the call to the tested method, and the verification code. Also remove the
failcall from the end, as it will make your test always fail 🙂