junit4 icon indicating copy to clipboard operation
junit4 copied to clipboard

Add an extension point to replace the test instance.

Open yschimke opened this issue 14 years ago • 6 comments

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

yschimke avatar Aug 02 '10 09:08 yschimke

We should (finally) fix this in 4.12

dsaff avatar Oct 24 '12 18:10 dsaff

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

nealeu avatar Dec 03 '12 08:12 nealeu

Unfortunately, I don't think we're going to (finally) fix this in 4.12.

dsaff avatar Sep 04 '13 22:09 dsaff

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

kcooney avatar Apr 17 '14 06:04 kcooney

In a rough way this sounds related to #1106 in that some redesign may be needed for the Rules system

EarthCitizen avatar Apr 06 '15 16:04 EarthCitizen

(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 before LifeCycleAwareRule would have referenced the (previous) test instance
  • Any TestRule that takes this 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.

kcooney avatar Aug 06 '17 23:08 kcooney