aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/org/junit/rules/Verifier.java
blob: be1a55ef31962f1d69ae3f57280b08625abfb46c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
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();
 *     
 *        &#064;Rule
 *        public MethodRule verifier = new Verifier() {
 *           &#064;Override public void verify() {
 *              assertTrue(errorLog.isEmpty());
 *           }
 *        }
 *        
 *        &#064;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 {
	}
}