I am using a JUnit Rule to immediately re-run any failed tests. My extra requirement is, if the re-run also fails, determine whether they failed for the same reason.
To do this I've adapted the code from this answer to keep a record of the failures and compare them. However, the comparison (.equals) always evaluates to false despite them failing for the same reason. What is the best way to go about this?
private Statement statement(final Statement base, final Description description) {
return new Statement() {
@Override
public void evaluate() throws Throwable {
for (int i = 0; i < retryCount; i++) {
try {
base.evaluate();
return;
} catch (Throwable t) {
System.err.println(description.getDisplayName() + ": run " + (i + 1) + " failed");
// Compare this error with the one before it.
if (errors.size() > 0) {
if (t.equals(errors.get(errors.size() - 1))) {
System.out.println("The error is the same as the previous one!");
} else {
System.out.println("The error is different from the previous one.");
}
}
errors.add(t);
}
}
System.err.println(description.getDisplayName() + ": giving up after " + retryCount
+ " failures");
// Throw most recent error.
throw errors.get(errors.size() - 1);
}
};
}