-
Notifications
You must be signed in to change notification settings - Fork 583
Improved JUnit integration with PowerMockRunnerDelegate annotation #6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Improved JUnit integration with PowerMockRunnerDelegate annotation #6
Conversation
…lease MainMockTransformer from the responsibility of the "type.detach()"-stuff
…nit-integration. The idea is to have this new notifier responsible for firing the PowerMockTestListener events. The JUnit-runner will still be responsible for firing the core JUnit-events to this new notifier - but the extra information necessary for proper composing and firing of PowerMockTestListener events will be fed through the new static utility GlobalNotificationBuildSupport!
…hancements for two purposes... 1) Make test-class static initializer and constructor send crucial details (for PowerMockTestListener events) to GlobalNotificationBuildSupport so that this information can be forwarded to whichever facility is used for composing the PowerMockTestListener events. 2) Removal of test-method annotations as a mean to achieve test-suite chunking!
…e AbstractTestSuiteChunkerImpl able to hand over some responsiblities to TestClassTransformer
…n-Powermock stuff taken care of by a JUnit-runner of your choice - or by one of JUnit's default runner-implementations. On top of the obvious benefit (PowerMock-features in combination with other JUnit runners) this could also help future maintenance of PowerMock as potential new JUnit features no longer need to be reproduced in any additional PowerMockJUnit4xRunnerDelegateImpl-class. E.g. PowerMockRunnerDelegate-usage will result in (unconscious) support for JUnit-rules as long as JUnit-4.7 or later is on classpath, even though the mechanisms around the PowerMockRunnerDelegate-annotation know nothing about JUnit rules! This is a solution where PowerMock stays in the background and deals with PowerMock-specific stuff whereas the general JUnit stuff is handed over to JUnit's defult runner - or another runner that is specified by the annotation PowerMockRunnerDelegate. The mechanism is triggered by adding the PowerMockRunnerDelegate-annotation on the test-class - or by running the test with JVM system-property "-Dpowermock.implicitDelegateAnnotation=true"
…plicit usage of the mechanisms around the new annotation "PowerMockRunnerDelegate"
…g tests with system-property "-Dpowermock.implicitDelegateAnnotation=true" - used to cause a test failure with the reasonable message "No runnable methods". The failure was fired by JUnit's default runner. This trivial problem was solved by making TestParent abstract.
…st-suite. E.g. org.powermock.examples.dom4j.AbstractXMLRequestCreatorBaseTest would otherwise fail when run with system-property "-Dpowermock.implicitDelegateAnnotation=true" because of the class-loader's VIP-treatment of package-names that start with "org.powermock.". The special treatment of "org.powermock."-classes is necessary for Powermock to work properly. This problem occurs because some of the demo tests in PowerMock's test-suite is in such a package as well. The quick solution was to treat "org.powermock.examples.*" packages just like any other packages. This is of course an ugly workaround but the other obvious solution (more classes in test-suite to other packages) is also kind of ugly.
…nisms around the PowerMockDelegateRunner annotation allow integration with many different JUnit-runners. Many of the tests are remakes (more or less) of tests from Powermock agent/rule test suites.
…wermockRunnerDelegate
Wow! I need to look into the pull request some more before merging since it's so big but it seems very promising. @PowerMockRunnerDelagate seems awesome. And reducing the JUnit internals is just so great. Tests seem to work well when I run them om my machine so it's probably a hiccup of some sort at Travis?! |
The Travis hiccup can occasionally occur for me as well - on all branches! It is the test "EqualsWithGetClassTest" that fails if test-method #callingGetClassOnAMockFailsWhenTheCallWasUnexpectedAndMockStandardMethodsIsSet() happens to run before the other test-method #callingGetClassOnAMockWorksWhenTheCallWasUnexpected(), which usually goes first on my machine but Travis seems to launch these test-methods in the opposite order. It is possible to reproduce the failure on every test-run by cancelling the test-method that needs to go first: // @test After having confirmed that this failure can occur on all branches I have not looked any deeper into this. |
Improved JUnit integration with PowerMockRunnerDelegate annotation
I'm so sorry for taking so long but I've finally got around to merge this. This is probably one of the best pull requests in the history of PowerMock :) I'm amazed! PowerMock is probably not the easiest project to get into and you've done a great job! I would be really happy if you have more ideas or want to help out more in the future. The ability to use "powermock.implicitDelegateAnnotation=true" for testing is also just great! |
Merge change from main project.
PowerMockRunner of PowerMock-1.5.x achieves JUnit-integration by delegating to one of its PowerMockJUnit4[479]RunnerDelegateImpl, which fulfills its duties by reproducing the core JUnit functionality.
This pull-request opens a way for PowerMockRunner to concentrate on PowerMock-features (static-mocking etc) and leave the actual test-execution to JUnit's default runner (or another runner of your choice). This allows PowerMock to (without any modifications) support future new JUnit-features from day one! - And from there it was only a small step to make PowerMockRunner support delegation to any other runner-implementation, which can be specified by the new feature annotation "PowerMockRunnerDelegate".
In short there are two benefits:
Faster progress for PowerMock development as new JUnit features can be supported with no development effort.
PowerMock users can have PowerMockRunner delegate test-execution to a runner of their choice. The pull-request include example tests on delegation to "SpringJUnit4ClassRunner", "Parameterized", "Enclosed" and a "selfie"-runner.
I know that 2 can also be achieved by using PowerMockRule but @PowerMockRunnerDelagate has one obvious advantage: - Delegation can be made to a 3rd-party runner that does not support rules, whereas PowerMockRule can never work with a runner that does not have rule-support.
Also - AFAIK PowerMockRule supports almost all PowerMock features but there are not so many tests in the project code-base. The delegation solution in this pull-request can be activated with the system-property "powermock.implicitDelegateAnnotation=true", which is set by the new build-profile "implicitDelegateAnnotation" (in the root pom.xml). Therewith it is easily tested by running the old existing tests with the "implicitDelegateAnnotation" profile, whereas PowerMockRule needs separate test-classes.
I hope you like this pull-request and have it included in PowerMock's next feature release.