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 @@ *
  • {@link Verifier}: fail test if object state ends up incorrect
  • * * - * Note that {@link MethodRule} is now deprecated, you should be using {@link TestRule} instead. + * Note that {@link MethodRule} has been replaced by {@link TestRule}, + * which has the added benefit of supporting class rules. * * @since 4.7 */ -@Deprecated public interface MethodRule { /** * Modifies the method-running {@link Statement} to implement an additional diff --git a/src/main/java/org/junit/rules/TestWatchman.java b/src/main/java/org/junit/rules/TestWatchman.java index f07e08667bd9..57b8394732af 100644 --- a/src/main/java/org/junit/rules/TestWatchman.java +++ b/src/main/java/org/junit/rules/TestWatchman.java @@ -39,8 +39,7 @@ * * * @since 4.7 - * @deprecated {@link MethodRule} is deprecated. - * Use {@link TestWatcher} implements {@link TestRule} instead. + * @deprecated Use {@link TestWatcher} (which implements {@link TestRule}) instead. */ @Deprecated public class TestWatchman implements MethodRule { diff --git a/src/main/java/org/junit/runners/BlockJUnit4ClassRunner.java b/src/main/java/org/junit/runners/BlockJUnit4ClassRunner.java index efcd93b1175a..1b73d27e0244 100644 --- a/src/main/java/org/junit/runners/BlockJUnit4ClassRunner.java +++ b/src/main/java/org/junit/runners/BlockJUnit4ClassRunner.java @@ -342,7 +342,6 @@ private Statement withRules(FrameworkMethod method, Object target, return result; } - @SuppressWarnings("deprecation") private Statement withMethodRules(FrameworkMethod method, List testRules, Object target, Statement result) { for (org.junit.rules.MethodRule each : getMethodRules(target)) { @@ -353,7 +352,6 @@ private Statement withMethodRules(FrameworkMethod method, List testRul return result; } - @SuppressWarnings("deprecation") private List getMethodRules(Object target) { return rules(target); } @@ -362,11 +360,7 @@ private List getMethodRules(Object target) { * @param target the test case instance * @return a list of MethodRules that should be applied when executing this * test - * @deprecated {@link org.junit.rules.MethodRule} is a deprecated interface. Port to - * {@link TestRule} and - * {@link BlockJUnit4ClassRunner#getTestRules(Object)} */ - @Deprecated protected List rules(Object target) { return getTestClass().getAnnotatedFieldValues(target, Rule.class, org.junit.rules.MethodRule.class);