Nedeni basittir, TestRule
MethodRule
'un yerini alması planlanmıştır. MethodRule
4.7 uygulanan kişiye ve bir yöntemi ile bir arayüz edilmiştir:
Statement apply(Statement base, FrameworkMethod method, Object target)
FrameworkMethod
ilk etapta maruz olmamalıdır (neredeyse) bir iç JUnit sınıfıdır. object
, yöntemin çalıştırılacağı nesnedir; örneğin, yansımayı kullanarak testin durumunu değiştirebilirsiniz.
TestRule
4.9 tanıtılan, ancak,:
Statement apply(Statement base, Description description)
Description
test açıklamasını içeren bir değişmez POJO olup. Bir test içindeki durumu değiştirmenin yolu, TestRule
kullanarak test içinde doğru şekilde kapsüllenmektir. Tamamen daha temiz bir tasarım.
TestWatchman(MethodRule)
ve TestWatcher(TestRule)
arasındaki özel fark, TestWatcher'ın daha iyi hata işleme sahip olması dışında, en düşüktür, bu nedenle bu tercih kullanılmalıdır. Her ikisi de succeeded()
, failed()
, starting()
, finished()
gibi geçersiz kılma yöntemlerine sahiptir.
public static class WatchmanTest {
private static String watchedLog;
@Rule
public TestWatcher watchman= new TestWatcher() {
@Override
protected void failed(Throwable e, Description description) {
watchedLog+= description + "\n";
}
@Override
protected void succeeded(Description description) {
watchedLog+= description + " " + "success!\n";
}
};
@Test
public void fails() {
fail();
}
@Test
public void succeeds() {
}
}
TestWatcher(TestRule)
overidden yöntemlerinde istisnalar işler. İstisnalar atılırsa, testin yerine getirilmesinden sonra test yöntemi başarısız olur.
fazla bilgi için, JUnit sürümü 4.11 için sürüm notlarına göre TestWatcher ve
TestWatchman bkz: "MethodRule artık kullanımdan kaldırıldı" Https://github.com/KentBeck/junit/blob/master/doc/ReleaseNotes4.11.md – sversch