1package org.junit.rules; 2 3import org.junit.runner.Description; 4import org.junit.runners.model.Statement; 5 6/** 7 * Verifier is a base class for Rules like ErrorCollector, which can turn 8 * otherwise passing test methods into failing tests if a verification check is 9 * failed 10 * 11 * <pre> 12 * public static class ErrorLogVerifier() { 13 * private ErrorLog errorLog = new ErrorLog(); 14 * 15 * @Rule 16 * public MethodRule verifier = new Verifier() { 17 * @Override public void verify() { 18 * assertTrue(errorLog.isEmpty()); 19 * } 20 * } 21 * 22 * @Test public void testThatMightWriteErrorLog() { 23 * // ... 24 * } 25 * } 26 * </pre> 27 */ 28public class Verifier implements TestRule { 29 public Statement apply(final Statement base, Description description) { 30 return new Statement() { 31 @Override 32 public void evaluate() throws Throwable { 33 base.evaluate(); 34 verify(); 35 } 36 }; 37 } 38 39 /** 40 * Override this to add verification logic. Overrides should throw an 41 * exception to indicate that verification failed. 42 */ 43 protected void verify() throws Throwable { 44 } 45} 46