Eduards Sizovs opened SPR-10252 and commented
In some situations it's convenient to inject dependencies into JUnit's @Rule and @ClassRule managed class' instances. Currently it can be achieved by extending DependencyInjectionTestExecutionListener (DITEL) with additional logic that looks for @Rule and @ClassRule fields and autowires them the same way as test instance is autowired. Even given the opportunity to extend class, I consider this functionality common enough.
Affects: 3.2.1
Issue Links:
5 votes, 6 watchers
Ken Wang commented
This feature would be really nice to have. Especially since Rules are instantiated before the test beans are autowired, one cannot even supply the dependencies in the Rule constructors.
Sam Brannen commented
FWIW, if you are using the SpringJUnit4ClassRunner (or SpringRunner) you can actually already inject dependencies into @Rule instances if you register the rule as a bean in the application context and annotate the field in the test class with @Autowired in addition to @Rule.
That's a _trick_ I've seen used in various projects.
Ken Wang commented
Interesting, I'll give that a try. Do you think @Configurable would work as well?
Sam Brannen commented
Do you think
@Configurablewould work as well?
To be honest, I haven't put much thought into that as an option, and I can imagine that there might be a timing issue there.
So, if you'd be willing to give it a shot and report back here on your findings, that would be greatly appreciated! ;-)
Ken Wang commented
My my testing, it does not.
Sam Brannen commented
Please note that this issue is related to #20481 and this answer on Stack Overflow.
Any chance of this ever happening?
Most helpful comment
Any chance of this ever happening?