• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 package org.junit;
2 
3 import java.lang.annotation.ElementType;
4 import java.lang.annotation.Retention;
5 import java.lang.annotation.RetentionPolicy;
6 import java.lang.annotation.Target;
7 
8 /**
9  * Annotates static fields that reference rules or methods that return them. A field must be public,
10  * static, and a subtype of {@link org.junit.rules.TestRule}.  A method must be public static, and return
11  * a subtype of {@link org.junit.rules.TestRule}.
12  * <p>
13  * The {@link org.junit.runners.model.Statement} passed
14  * to the {@link org.junit.rules.TestRule} will run any {@link BeforeClass} methods,
15  * then the entire body of the test class (all contained methods, if it is
16  * a standard JUnit test class, or all contained classes, if it is a
17  * {@link org.junit.runners.Suite}), and finally any {@link AfterClass} methods.
18  * <p>
19  * The statement passed to the {@link org.junit.rules.TestRule} will never throw an exception,
20  * and throwing an exception from the {@link org.junit.rules.TestRule} will result in undefined
21  * behavior.  This means that some {@link org.junit.rules.TestRule}s, such as
22  * {@link org.junit.rules.ErrorCollector},
23  * {@link org.junit.rules.ExpectedException},
24  * and {@link org.junit.rules.Timeout},
25  * have undefined behavior when used as {@link ClassRule}s.
26  * <p>
27  * If there are multiple
28  * annotated {@link ClassRule}s on a class, they will be applied in an order
29  * that depends on your JVM's implementation of the reflection API, which is
30  * undefined, in general. However, Rules defined by fields will always be applied
31  * before Rules defined by methods.
32  * <p>
33  * For example, here is a test suite that connects to a server once before
34  * all the test classes run, and disconnects after they are finished:
35  * <pre>
36  * &#064;RunWith(Suite.class)
37  * &#064;SuiteClasses({A.class, B.class, C.class})
38  * public class UsesExternalResource {
39  *     public static Server myServer= new Server();
40  *
41  *     &#064;ClassRule
42  *     public static ExternalResource resource= new ExternalResource() {
43  *       &#064;Override
44  *       protected void before() throws Throwable {
45  *          myServer.connect();
46  *      }
47  *
48  *      &#064;Override
49  *      protected void after() {
50  * 	        myServer.disconnect();
51  *      }
52  *   };
53  * }
54  * </pre>
55  * <p>
56  * and the same using a method
57  * <pre>
58  * &#064;RunWith(Suite.class)
59  * &#064;SuiteClasses({A.class, B.class, C.class})
60  * public class UsesExternalResource {
61  *     public static Server myServer= new Server();
62  *
63  *     &#064;ClassRule
64  *     public static ExternalResource getResource() {
65  *         return new ExternalResource() {
66  *             &#064;Override
67  *             protected void before() throws Throwable {
68  *                 myServer.connect();
69  *             }
70  *
71  *             &#064;Override
72  *             protected void after() {
73  *                 myServer.disconnect();
74  *             }
75  *         };
76  *     }
77  * }
78  * </pre>
79  * <p>
80  * For more information and more examples, see {@link org.junit.rules.TestRule}.
81  *
82  * @since 4.9
83  */
84 @Retention(RetentionPolicy.RUNTIME)
85 @Target({ElementType.FIELD, ElementType.METHOD})
86 public @interface ClassRule {
87 }
88