私は最近、 のソース コードを掘り下げています。JUnit-4.11混乱しているのは、一見冗長なProtectableインターフェイスです。宣言は次のとおりです。
public interface Protectable {
public abstract void protect() throws Throwable;
}
TestResultクラスには、次のように匿名インスタンスを実現するメソッドvoid run(final TestCase test)がProtectableあります。
protected void run(final TestCase test) {
startTest(test);
Protectable p = new Protectable() {
public void protect() throws Throwable {
test.runBare();
}
};
runProtected(test, p);
endTest(test);
}
runProtected方法は次のとおりです。
public void runProtected(final Test test, Protectable p) {
try {
p.protect();
} catch (AssertionFailedError e) {
addFailure(test, e);
} catch (ThreadDeath e) { // don't catch ThreadDeath by accident
throw e;
} catch (Throwable e) {
addError(test, e);
}
}
runProtectedおわかりのように、を実行するだけでは何test.runBare();をするのでしょうか。以下のようなコードを書くことができないのはなぜですか。
protected void run(final TestCase test) {
startTest(test);
test.runBare();
endTest(test);
}