Deprecate InjectMocks
See original GitHub issueI think no one of the core team is a fan of this feature. The implementation is not as full-fletched as other dependency injection mechanisms. At the time of writing this issue, there are 17 open issues describing various problems with @InjectMocks
. Moreover, as it solves a problem unrelated to mocking and there are different tools available to solve this use case, I propose we deprecate @InjectMocks
and schedule for removal in Mockito 3/4. (I think 4 is best, to keep 3 a minimal breaking change version to ease upgrades).
Issue Analytics
- State:
- Created 5 years ago
- Reactions:9
- Comments:9 (7 by maintainers)
Top Results From Across the Web
InjectMocks (Mockito 4.9.0 API) - javadoc.io
Minimizes repetitive mock and spy injection. Mockito will try to inject mocks only either by constructor injection, property injection or setter injection in ......
Read more >Difference between @Mock and @InjectMocks - Stack Overflow
Notice that that @InjectMocks are about to be deprecated. deprecate @InjectMocks and schedule for removal in ...
Read more >Using Mockito With JUnit 5 | Code With Arho
initMock() method, which is deprecated and replaced with ... Mockito to inject mocks automatically to a field annotated with @InjectMocks .
Read more >Warning: “The type MockitoJUnitRunner is deprecated”
Learn how to handle the MockitoJUnitRunner class deprecation ... Namely, the one referring to the deprecated MockitoJUnitRunner class.
Read more >Automatic instantiation of @Spies, @InjectMocks ... - CWIKI.US
Mockito will now try to instantiate @ Spy and will instantiate @ InjectMocks fields using constructor injection, setter injection, ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Did you receive any feedback @mockitoguy ?
It is a useful feature that has limitations we are not interested in addressing. Perhaps we should work on better error reporting / early failure when InjectMocks hits one of the limitations?
Do we aspire to build a “perfect” tool? We want to build a useful tool, that is “great enough”. In many other parts of our API we have caveats, typically caused by Java limitations. We always optimized to make the 95% case experience great, and remaining 5% decent (or not horrible 😉 - via good error messages, great documentation. Given Mockito popularity, this strategy has worked out.
Does removing this feature make Mockito better? I have a talk about Mockito 3 at CodeOne next week. I’ll ask the audience about InjectMocks.
Nice discussion! Thank you everybody for chiming in!