question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Deprecate InjectMocks

See original GitHub issue

I 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:open
  • Created 5 years ago
  • Reactions:9
  • Comments:9 (7 by maintainers)

github_iconTop GitHub Comments

2reactions
glureau-betcliccommented, Jan 30, 2019

Did you receive any feedback @mockitoguy ?

1reaction
mockitoguycommented, Oct 18, 2018

By having an inherently broken feature in our API, we are not living up to our promises imo.

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!

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found