sebb> This could be turned into a Wiki or other web-page so it can be
sebb>readily referenced and enhanced.
It could, however I have a secret desire to create a talk on that
topic, so I don't want to spoil it by publishing an article before the
talk :)
sebb> In this specific case, adding the value of 'i' to the message would help.
No, adding "i" won't help because it still won't explain WHY the
expected value is as provided.
In other words, "Incorrect entry at index 5. expected: d, actual: z"
is still a very bad assertion message.
Imagine a Bugzilla report: Bug 374562: Incorrect entry at index 5.
expected: d, actual: z
Longer version is below.
Here's the full code (see TestCSVSaveService):
private void checkStrings(String[] expected, String[] out) {
assertEquals("Incorrect number of strings
returned",expected.length, out.length);
for(int i = 0; i < out.length; i++){
assertEquals("Incorrect entry returned",expected[i], out[i]);
}
}
private void checkSplitString(String input, char delim, String[]
expected) throws Exception {
String[] out = CSVSaveService.csvSplitString(input, delim);.....
checkStrings(expected, out);
}
checkSplitString("a,bc,d,e", ',', new String[]{"a","bc","d","e"});
In fact, the assertions are "completely broken" (tm)
Let's assume we have a bug so the actual array is {"a", "bc,d", "e"}
checkStrings above would fail with "incorrect number of strings
returned expected: 4, got: 3"
Does it help? Not really.
Let's assume we have a bug so the actual array is {"a", "bc", "d", "z"}
checkStrings above would fail with "Incorrect entry returned expected: e got: z"
Does it help? Not really.
Does adding index to the message help? Not really.
The proper code would be to remove checkStrings completely, and update
checkSplitString as follows
private void checkSplitString(String input, char delim, String[]
expected) throws Exception {
String[] out = CSVSaveService.csvSplitString(input, delim);.....
assertEquals("csvSplitString(\""+input+"\", delim="+delim+")",
Arrays.toString(expected)
+ ", length=" + expected.length, Arrays.toString(out) +
", length=" + out.length);
}
In case of a failure it would print full expected output, full actual
value, and (it is very important!) it will print the input arguments
for csvSplitString.
So the failure would look like: csvSplitString("a,bc,d,e", delim=,)
expected: {a, bc, d, e}, length=4, actual: {a, bc, d, z}, length=4
Vladimir