diff --git a/src/main/java/org/junit/ClassRule.java b/src/main/java/org/junit/ClassRule.java index a1c2fcb68462..586fb84cf6dc 100644 --- a/src/main/java/org/junit/ClassRule.java +++ b/src/main/java/org/junit/ClassRule.java @@ -6,9 +6,10 @@ import java.lang.annotation.Target; /** - * Annotates static fields that contain rules or methods that return them. A field must be public, + * Annotates static fields that reference rules or methods that return them. A field must be public, * static, and a subtype of {@link org.junit.rules.TestRule}. A method must be public static, and return - * a subtype of {@link org.junit.rules.TestRule} + * a subtype of {@link org.junit.rules.TestRule}.
+ * * The {@link org.junit.runners.model.Statement} passed * to the {@link org.junit.rules.TestRule} will run any {@link BeforeClass} methods, * then the entire body of the test class (all contained methods, if it is @@ -36,20 +37,20 @@ * @RunWith(Suite.class) * @SuiteClasses({A.class, B.class, C.class}) * public class UsesExternalResource { - * public static Server myServer= new Server(); + * public static Server myServer= new Server(); * - * @ClassRule - * public static ExternalResource resource= new ExternalResource() { - * @Override - * protected void before() throws Throwable { - * myServer.connect(); - * }; + * @ClassRule + * public static ExternalResource resource= new ExternalResource() { + * @Override + * protected void before() throws Throwable { + * myServer.connect(); + * } * - * @Override - * protected void after() { - * myServer.disconnect(); - * }; - * }; + * @Override + * protected void after() { + * myServer.disconnect(); + * } + * }; * } * * @@ -59,19 +60,19 @@ * @RunWith(Suite.class) * @SuiteClasses({A.class, B.class, C.class}) * public class UsesExternalResource { - * public static Server myServer= new Server(); + * public static Server myServer= new Server(); * - * @ClassRule - * public static ExternalResource getResource() { - * return new ExternalResource() { - * @Override - * protected void before() throws Throwable { - * myServer.connect(); + * @ClassRule + * public static ExternalResource getResource() { + * return new ExternalResource() { + * @Override + * protected void before() throws Throwable { + * myServer.connect(); * } * - * @Override - * protected void after() { - * myServer.disconnect(); + * @Override + * protected void after() { + * myServer.disconnect(); * } * }; * } diff --git a/src/main/java/org/junit/Rule.java b/src/main/java/org/junit/Rule.java index 8899114d5a7d..429821a2b3e0 100644 --- a/src/main/java/org/junit/Rule.java +++ b/src/main/java/org/junit/Rule.java @@ -6,9 +6,12 @@ import java.lang.annotation.Target; /** - * Annotates fields that contain rules or methods that return a rule. A field must be public, not - * static, and a subtype of {@link org.junit.rules.TestRule}. A method must be public, not static - * and must return a subtype of {@link org.junit.rules.TestRule}. + * Annotates fields that reference rules or methods that return a rule. A field must be public, not + * static, and a subtype of {@link org.junit.rules.TestRule} (preferred) or + * {@link org.junit.rules.MethodRule}. A method must be public, not static, + * and must return a subtype of {@link org.junit.rules.TestRule} (preferred) or + * {@link org.junit.rules.MethodRule}.
+ * * The {@link org.junit.runners.model.Statement} passed * to the {@link org.junit.rules.TestRule} will run any {@link Before} methods, * then the {@link Test} method, and finally any {@link After} methods, @@ -17,21 +20,21 @@ * However, if there are mutliple fields (or methods) they will be applied in an order * that depends on your JVM's implementation of the reflection API, which is * undefined, in general. Rules defined by fields will always be applied - * before Rules defined by methods. + * before Rules defined by methods.
* * For example, here is a test class that creates a temporary folder before * each test method, and deletes it after each: * *
* public static class HasTempFolder { - * @Rule - * public TemporaryFolder folder= new TemporaryFolder(); + * @Rule + * public TemporaryFolder folder= new TemporaryFolder(); * - * @Test - * public void testUsingTempFolder() throws IOException { - * File createdFile= folder.newFile("myfile.txt"); - * File createdFolder= folder.newFolder("subfolder"); - * // ... + * @Test + * public void testUsingTempFolder() throws IOException { + * File createdFile= folder.newFile("myfile.txt"); + * File createdFolder= folder.newFolder("subfolder"); + * // ... * } * } *@@ -40,18 +43,18 @@ * *
* public static class HasTempFolder { - * private TemporaryFolder folder= new TemporaryFolder(); + * private TemporaryFolder folder= new TemporaryFolder(); * - * @Rule - * public TemporaryFolder getFolder() { - * return folder; + * @Rule + * public TemporaryFolder getFolder() { + * return folder; * } * - * @Test - * public void testUsingTempFolder() throws IOException { - * File createdFile= folder.newFile("myfile.txt"); - * File createdFolder= folder.newFolder("subfolder"); - * // ... + * @Test + * public void testUsingTempFolder() throws IOException { + * File createdFile= folder.newFile("myfile.txt"); + * File createdFolder= folder.newFolder("subfolder"); + * // ... * } * } *@@ -59,10 +62,6 @@ * For more information and more examples, see * {@link org.junit.rules.TestRule}. * - * Note: for backwards compatibility, this annotation may also mark - * fields or methods of type {@link org.junit.rules.MethodRule}, which will be honored. However, - * this is a deprecated interface and feature. - * * @since 4.7 */ @Retention(RetentionPolicy.RUNTIME) diff --git a/src/main/java/org/junit/rules/MethodRule.java b/src/main/java/org/junit/rules/MethodRule.java index 74952224c100..f6a8dd4328c4 100644 --- a/src/main/java/org/junit/rules/MethodRule.java +++ b/src/main/java/org/junit/rules/MethodRule.java @@ -24,11 +24,11 @@ *