001package org.junit.rules;
002
003import org.junit.runner.Description;
004import org.junit.runners.model.Statement;
005
006/**
007 * Verifier is a base class for Rules like ErrorCollector, which can turn
008 * otherwise passing test methods into failing tests if a verification check is
009 * failed
010 *
011 * <pre>
012 *     public static class ErrorLogVerifier {
013 *        private ErrorLog errorLog = new ErrorLog();
014 *
015 *        &#064;Rule
016 *        public Verifier verifier = new Verifier() {
017 *           &#064;Override public void verify() {
018 *              assertTrue(errorLog.isEmpty());
019 *           }
020 *        }
021 *
022 *        &#064;Test public void testThatMightWriteErrorLog() {
023 *           // ...
024 *        }
025 *     }
026 * </pre>
027 *
028 * @since 4.7
029 */
030public abstract class Verifier implements TestRule {
031    public Statement apply(final Statement base, Description description) {
032        return new Statement() {
033            @Override
034            public void evaluate() throws Throwable {
035                base.evaluate();
036                verify();
037            }
038        };
039    }
040
041    /**
042     * Override this to add verification logic. Overrides should throw an
043     * exception to indicate that verification failed.
044     */
045    protected void verify() throws Throwable {
046    }
047}