• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 package org.junit.rules;
2 
3 import org.junit.runner.Description;
4 import 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  *        &#064;Rule
16  *        public Verifier verifier = new Verifier() {
17  *           &#064;Override public void verify() {
18  *              assertTrue(errorLog.isEmpty());
19  *           }
20  *        }
21  *
22  *        &#064;Test public void testThatMightWriteErrorLog() {
23  *           // ...
24  *        }
25  *     }
26  * </pre>
27  *
28  * @since 4.7
29  */
30 public abstract class Verifier implements TestRule {
apply(final Statement base, Description description)31     public Statement apply(final Statement base, Description description) {
32         return new Statement() {
33             @Override
34             public void evaluate() throws Throwable {
35                 base.evaluate();
36                 verify();
37             }
38         };
39     }
40 
41     /**
42      * Override this to add verification logic. Overrides should throw an
43      * exception to indicate that verification failed.
44      */
45     protected void verify() throws Throwable {
46     }
47 }
48