junit4
junit4 copied to clipboard
Add an extension point to replace the test instance.
MethodRule only gets invoked for the test method, however for a change where you would otherwise extend BlockJUnit4ClassRunner and override withBefores, you can't achieve the same with a Rule.
MethodRule should have a sub-interface e.g. LifeCycleAwareRule that allows a rule to change the test instance before the @Before block is run.
One example is unitils @SpringBeanByType, these must be injected before the @Before block runs, but with Rule this is not possible
We should (finally) fix this in 4.12
Agree. A good measure would be to capture what Runners (such as Unitils and Spring ones) are just doing before class/before test combos, and ensure those can be converted to use Rules
Unfortunately, I don't think we're going to (finally) fix this in 4.12.
Considering that we don't yet have a concrete proposal for how we would fix this, I'm changing this bug to be targeted for 4.13
In a rough way this sounds related to #1106 in that some redesign may be needed for the Rules system
(going through issues targeting 4.13 to decide what to do with them)
MethodRule should have a sub-interface e.g. LifeCycleAwareRule that allows a rule to change the test instance before the @before block is run.
Unfortunately this would cause problems:
- Any
MethodRule
that was applied beforeLifeCycleAwareRule
would have referenced the (previous) test instance - Any
TestRule
that takesthis
as a parameter would stop working
I don't think we ever intended for a runner to pass a different test instance to `BlockJUnit4ClassRunner.withBefores(), etc).
This sounds like a feature request to allow an extra extension point that can be used to replace the test instance before any rule is executed. I'm renaming the issue to indicate that; if that's not was was intended please let us know.
In any case, I don't think we will do this in 4.13 either, so removing the milestone.