KentBeck/junit — Issue #357: Incompatibility of 4.10 MethodRule
Author: KyleRogers
Created: 2011-10-30 20:41:45+00:00
State: open

I'm not happy with the incompatible changes done to the built-in junit rules (Verifier for example). The problem is the removal of the MethodRule interface from those classes.

There are two problems with that:

### Upgrading from 4.9 to 4.10 means refactoring

If you've used the built-in rules as mentioned in their own javadoc you'll get compile errors:

```java
@Rule
public final MethodRule timeout = new Timeout(30);
```
won't compile because Timeout does not implement MethodRule anymore (but implements TestRule instead).

That is okay if you own the code but can be lot of work if you have lots of tests! Nevertheless if anyone has that issue I'd recommend refactoring to

```java
@Rule
public final Timeout timeout = new Timeout(30);
```
if you implement a library that might be used with junit pre-4.10 as well as junit 4.10 (and later).

### Libraries can't be compatible with JUnit 4.9 and 4.10

The above might be okay if you own the code. But imagine you're using a third party library. If the library uses the TimeoutRule or the Verifier as mentioned above, you'll have the choice between dropping the third party library or beeing stuck with junit 4.9 and postpone the update until the library has.

Additionally as a library developer you have to choose between supporting either JUnit 4.9 and below or JUnit 4.10 . You cannot support both at a time. Thats why: You cannot implement both, TestRule **and** MethodRule because that would break compatibility with 4.9 where TestRule does not exist. But implementing MethodRule only is no solution either: Such rules cannot be used together with the @ClassRule annotation. The RuleFieldValidator will *not* complain but those fields or methods annotated with @ClassRule and returning MethodRule implementations will not be run but will be silently ignored.

### What could be done?
The built-in rules could implement both, MethodRule and TestRule. The @ClassRule annotation should honor MethodRule as well. This would allow library developers to compile with 4.9 but library users could still choose between 4.9 or below and 4.11 or above ;-)