diff options
Diffstat (limited to 'src/org/junit/rules/Verifier.java')
-rw-r--r-- | src/org/junit/rules/Verifier.java | 45 |
1 files changed, 45 insertions, 0 deletions
diff --git a/src/org/junit/rules/Verifier.java b/src/org/junit/rules/Verifier.java new file mode 100644 index 0000000..be1a55e --- /dev/null +++ b/src/org/junit/rules/Verifier.java @@ -0,0 +1,45 @@ +package org.junit.rules; + +import org.junit.runner.Description; +import org.junit.runners.model.Statement; + +/** + * Verifier is a base class for Rules like ErrorCollector, which can turn + * otherwise passing test methods into failing tests if a verification check is + * failed + * + * <pre> + * public static class ErrorLogVerifier() { + * private ErrorLog errorLog = new ErrorLog(); + * + * @Rule + * public MethodRule verifier = new Verifier() { + * @Override public void verify() { + * assertTrue(errorLog.isEmpty()); + * } + * } + * + * @Test public void testThatMightWriteErrorLog() { + * // ... + * } + * } + * </pre> + */ +public class Verifier implements TestRule { + public Statement apply(final Statement base, Description description) { + return new Statement() { + @Override + public void evaluate() throws Throwable { + base.evaluate(); + verify(); + } + }; + } + + /** + * Override this to add verification logic. Overrides should throw an + * exception to indicate that verification failed. + */ + protected void verify() throws Throwable { + } +} |