tiebin-zhang / powermock

Automatically exported from code.google.com/p/powermock
Apache License 2.0
0 stars 0 forks source link

MethodRule and TestRule both need to be supported for junit >=4.9 #419

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?

@RunWith(PowerMockRunner.class)
public class RuleJunit47Test {
  @Rule
  public MyRule rule = new MyRule();
  @Test
  public void test() {
  }
  private class MyRule implements MethodRule {
    public Statement apply(Statement base, FrameworkMethod method, Object target) {
      return base;
    }
  }
}

What is the expected output? What do you see instead?

Tests in error: 
  test(RuleJunit47Test): MyRule cannot be cast to org.junit.rules.TestRule

What version of the product are you using? On what operating system?
powermock 1.5

Please provide any additional information below.
The interface org.junit.rules.MethodRule was deprecated in junit 4.9 and 4.10, 
I guess that's why there are two different JUnit-Runners in Powermock, one for 
>=4.7 which supports MethodRule and one for >=4.9 which supports TestRule.
Dropping support for a deprecated interface was OK, because one had to migrate 
to the new TestRule anyway. But with junit 4.11 the deprecation of MethodRule 
was dropped again, so that its now OK to implement MethodRule or TestRule for 
own Rules.

This change is not by accident, but even listed in the changelog:

> MethodRule is no longer deprecated.
https://github.com/KentBeck/junit/blob/master/doc/ReleaseNotes4.11.md

I made a patch which changes PowerMockJUnit49RunnerDelegateImpl to extend the 
4.7-runner (instead of the 4.4-runner). The 4.9-runner will now support both 
TestRule AND MethodRule, while the 4.7-runner continues to support only 
MethodRule, because TestRule is not available in the older version.
The patch removes quiet a lot from PowerMockJUnit49RunnerDelegateImpl, but this 
is only because most of the functionality seemed duplicated and could be 
removed after subclassing PowerMockJUnit47RunnerDelegateImpl. I hope I did not 
miss anything.

Original issue reported on code.google.com by boris-c...@gmx.com on 8 Jan 2013 at 8:20

Attachments:

GoogleCodeExporter commented 9 years ago
Thanks for the patch, I was not aware of this actually. All tests seem to run 
fine so I hope that it's ok. I've committed it to trunk but I needs further 
testing before I can feel really confident. Would you be so kind to try it out 
on older JUnit versions as well? (That would REALLY help a lot!) I'd like to 
know if it still _really_ works for JUnit 4.9/4.10 and 4.7.

Original comment by johan.ha...@gmail.com on 22 Jan 2013 at 7:43

GoogleCodeExporter commented 9 years ago
I just ran my testcase with MethodRule on all junit-versions from 4.7 (the 
version which introduced the feature) up to 4.11 (current). It worked for all 
of them with the 1.5.1-SNAPSHOT I compiled from the current trunk.

I also did the same test with TestRule on all junit-versions from 4.9 to 4.11. 
This worked in all combinations.

I also checked that the rule was actually executed in all cases.

Original comment by boris-c...@gmx.com on 24 Jan 2013 at 6:44

GoogleCodeExporter commented 9 years ago
Thanks a lot Boris, it's really appreciated. It's hard to maintain a project on 
your own so it's very important that people help out like you just did! Thanks 
again.

Original comment by johan.ha...@gmail.com on 25 Jan 2013 at 6:52