• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 package org.junit.rules;
2 
3 import org.junit.Rule;
4 import org.junit.runners.model.FrameworkMethod;
5 import org.junit.runners.model.Statement;
6 
7 /**
8  * A MethodRule is an alteration in how a test method is run and reported.
9  * Multiple {@link MethodRule}s can be applied to a test method. The
10  * {@link Statement} that executes the method is passed to each annotated
11  * {@link Rule} in turn, and each may return a substitute or modified
12  * {@link Statement}, which is passed to the next {@link Rule}, if any. For
13  * examples of how this can be useful, see these provided MethodRules,
14  * or write your own:
15  *
16  * <ul>
17  *   <li>{@link ErrorCollector}: collect multiple errors in one test method</li>
18  *   <li>{@link ExpectedException}: make flexible assertions about thrown exceptions</li>
19  *   <li>{@link ExternalResource}: start and stop a server, for example</li>
20  *   <li>{@link TemporaryFolder}: create fresh files, and delete after test</li>
21  *   <li>{@link TestName}: remember the test name for use during the method</li>
22  *   <li>{@link TestWatchman}: add logic at events during method execution</li>
23  *   <li>{@link Timeout}: cause test to fail after a set time</li>
24  *   <li>{@link Verifier}: fail test if object state ends up incorrect</li>
25  * </ul>
26  *
27  * Note that {@link MethodRule} has been replaced by {@link TestRule},
28  * which has the added benefit of supporting class rules.
29  *
30  * @since 4.7
31  */
32 public interface MethodRule {
33     /**
34      * Modifies the method-running {@link Statement} to implement an additional
35      * test-running rule.
36      *
37      * @param base The {@link Statement} to be modified
38      * @param method The method to be run
39      * @param target The object on which the method will be run.
40      * @return a new statement, which may be the same as {@code base},
41      *         a wrapper around {@code base}, or a completely new Statement.
42      */
apply(Statement base, FrameworkMethod method, Object target)43     Statement apply(Statement base, FrameworkMethod method, Object target);
44 }
45