我正在使用 JUnit 规则来立即重新运行任何失败的测试。我的额外要求是,如果重新运行也失败,请确定它们是否因相同原因而失败。
为此,我调整了此答案中的代码,以记录故障并进行比较。但是,比较 (.equals) 始终评估为 false,尽管它们由于相同的原因而失败。解决此问题的最佳方法是什么?
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);
}
};
}