Comments (7)
Far overdue, but for what it's worth: I quoted your reaction at the end of my article, under heading "Edit July 2020":
https://tedvinke.wordpress.com/2014/02/13/mockito-why-you-should-not-use-injectmocks-annotation-to-autowire-fields/#edit-july-2020
from mockito.
Hi,
tl;dr It would probably be possible to add a boolean flag in the annotation (a PR is welcome). Or today it's possible to change the AnnotationEngine
via the IMockitoConfiguration
, and then provide a tweaked engine for one's needs.
longer answer
Comments on this blog post are closed, so I'm writing here.
This feature has history and is designed that way for a reason.
The author think or say people think that Mockito injection somehow follows Spring's @Autowired
annotation processing, it just isn't!
People like the way how Mockito is able to mock Spring’s auto-wired fields with the
@InjectMocks
annotation.
Nowhere in the javadoc, Spring, @Autowired
, @Inject
, etc is mentioned. And the shown behavior is documented. In that sense this blog post advocates to read the javadoc when one is using some product!
This feature was implemented when annotation based injection wasn't even there in spring or in JEE.
And anyway the code being tested may even not live inside a Spring container, the code may even be legacy, i.e. without proper separation of concerns, the code may have various valid combination to initialize the bean (setters, fields), the code may have no tests at all. Still when one have to test these kind of code be it the full object or just a portion, it was kinda useful to have a framework that just did the boilerplate code.
Also note that so many Spring beans or maybe custom WhateverCompany beans have optional setters, i.e. they have default component.
So yes it fails silently, because Mockito is not able to confirm an object is correctly initialized or not when this object relies on fields/setters, it's just impossible. And yes constructor injection is probably the best and the correct approach to dependency injection as the author even suggest (as a reminder @InjectMocks
tries first to use constructor injection) ; I totally agree that the bean-y way (ie via fields/setters) to design object initialization is flawed.
The bottom line being @InjectMocks
feature in mockito was never designed to be a full featured dependency injection mechanism and will probably never be. At best it's shorthand way to inject mocks, but that's all.
from mockito.
Thank you for taking the time to explain. I think I may get ambitious this weekend and create a pull request.
from mockito.
No problem :)
Bugs happen all the time, but if it's in the javadoc, there's usually a valid reason. This post criticize the choices but point the wrong thing i.e. mockito for the reason I explained above.
Nowaday it may be reasonable to propose a boolean
flag that can check that all fields are injected with a mock.
from mockito.
Some grave digging: What about some argument for @InjectMocks
to limit it to certain kinds of initialization? Like @InjeckMocks(via = org.mockito.whatevs.ConstructorInjection)
and let it fail if that didn't work? Adding new parameters to constructors should then explcitly fail a test (unless you mocked the new thing before already - for some reason).
Maybe even combine it with a strategy to choose the constructor to tackle #1935 ? @InjectMocks(strategy = org.mockito.whatevs.strategies.LongestParamList
and MatchingSignature
(trying to resolve the constructor with the mocks provided in the test) and maybe more. @bric3
from mockito.
@black-snow This is an interesting approach ; I believe this is worth a discussion, possibly leading to a prototype. That said I struggled to actually contribute to this project in the last years. Even professionally I now rarely use Mockito. I might be able to review such code though.
from mockito.
Aight, guess I should open a new issue then.
from mockito.
Related Issues (20)
- @Mock and MockedConstruction
- application HOT 1
- In TypeBasedCandidateFilter.isCompatibleTypes -> ClassCastException: class TypeVariableImpl cannot be cast to class Class HOT 9
- Spy does not see method called in feedback call HOT 3
- Mockito.mock(...) does not work inside an Arquillian integration test anymore HOT 1
- Feature Request: MockedConstruction needs a setInitializer method
- ArgumentMatcher like assertArg() for multiple different calls? HOT 3
- Mockito does not work on Java 21 HOT 3
- Mockito.mock() not mocking the Class in Java 17 HOT 1
- ArgumentMatcher for one property
- Tests failures after adding mockito-inline on Java 21
- springjpa+junit5+mockito+querydsl
- ungraceful failure from instrumented code
- AdditionalMatchers.and() and or() swap matcher order HOT 5
- Failing while building mockito-inline version 4.6.1 using ./gradlew build (org.mockito:mockito-inline:4.6.1) HOT 1
- Mockito does not work with OTEL extension api in classpath
- verify can't differ/handle logging methods with "Object... arguments" in signature
- Not possible to mock Thread.sleep
- WIthMockito feature request
- potential editorconfig enhancement HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from mockito.