1package org.junit; 2 3import java.lang.annotation.ElementType; 4import java.lang.annotation.Retention; 5import java.lang.annotation.RetentionPolicy; 6import java.lang.annotation.Target; 7 8/** 9 * Annotates static fields that contain rules. Such a field must be public, 10 * static, and a subtype of {@link org.junit.rules.TestRule}. 11 * The {@link org.junit.runners.model.Statement} passed 12 * to the {@link org.junit.rules.TestRule} will run any {@link BeforeClass} methods, 13 * then the entire body of the test class (all contained methods, if it is 14 * a standard JUnit test class, or all contained classes, if it is a 15 * {@link org.junit.runners.Suite}), and finally any {@link AfterClass} methods. 16 * 17 * The statement passed to the {@link org.junit.rules.TestRule} will never throw an exception, 18 * and throwing an exception from the {@link org.junit.rules.TestRule} will result in undefined 19 * behavior. This means that some {@link org.junit.rules.TestRule}s, such as 20 * {@link org.junit.rules.ErrorCollector}, 21 * {@link org.junit.rules.ExpectedException}, 22 * and {@link org.junit.rules.Timeout}, 23 * have undefined behavior when used as {@link ClassRule}s. 24 * 25 * If there are multiple 26 * annotated {@link ClassRule}s on a class, they will be applied in an order 27 * that depends on your JVM's implementation of the reflection API, which is 28 * undefined, in general. 29 * 30 * For example, here is a test suite that connects to a server once before 31 * all the test classes run, and disconnects after they are finished: 32 * 33 * <pre> 34 * 35 * @RunWith(Suite.class) 36 * @SuiteClasses({A.class, B.class, C.class}) 37 * public class UsesExternalResource { 38 * public static Server myServer= new Server(); 39 * 40 * @ClassRule 41 * public static ExternalResource resource= new ExternalResource() { 42 * @Override 43 * protected void before() throws Throwable { 44 * myServer.connect(); 45 * }; 46 * 47 * @Override 48 * protected void after() { 49 * myServer.disconnect(); 50 * }; 51 * }; 52 * } 53 * </pre> 54 * 55 * For more information and more examples, see {@link org.junit.rules.TestRule}. 56 */ 57@Retention(RetentionPolicy.RUNTIME) 58@Target({ElementType.FIELD}) 59public @interface ClassRule { 60} 61