I'd like the Mockito @InjectMocks mechanism to resemble my DI
framework injection service. Like for example I'd probably chose my
required dependencies to be injected via a ctor, and the optional ones
via setters.
I wrote a small utility to test out some popular frameworks to see how
do they behave when asked to inject collaborators.
Here is what happens when all of the dependencies are satisfied.
[report] Vanilla Java
[report] Collaborator injected by constructor IS NULL
[report] Collaborator injected by field IS NULL
[report] Collaborator injected by setter IS NULL
[report] Spring
[report] Collaborator injected by constructor is not null
[report] Collaborator injected by field is not null
[report] Collaborator injected by setter is not null
[report] Guice
[report] Collaborator injected by constructor is not null
[report] Collaborator injected by field is not null
[report] Collaborator injected by setter is not null
[report] CDI
[report] Collaborator injected by constructor is not null
[report] Collaborator injected by field is not null
[report] Collaborator injected by setter is not null
[report] Mockito
[report] Collaborator injected by constructor is not null
[report] Collaborator injected by field IS NULL
[report] Collaborator injected by setter IS NULL
Have a look at:
https://github.com/marekdec/framework-injection-tester
The actual report generator can be found at
https://github.com/marekdec/framework-injection-tester/blob/master/framework-injection-tester/src/main/java/com/wordpress/marekdec/injectiontester/InjectionTester.java
The results are quite similar if some dependencies are missing i.e.
Spring, Weld and Guice just fail to boot, Mockito on the other hand,
does not report an error (which is understandable).
As the DI done by a test framework is sort of a controversial feature,
I guess you implemented it this way for some good reason.
Anyhow, why isn't the Mockito DI mechanism just like the others? What
was the rationale for this decision?
Marek
On Feb 1, 3:05 pm, Szczepan Faber <
szcze...@gmail.com> wrote:
> Hmmm, interesting. I kind of like the idea of a different annotation that
> would be using constructors and fail eagerly when no matching constructor
> can be found.
>
> I'm also thinking if we shouldn't make the @InjectMocs work more leniently,
> e.g:
> 1. try constructor
> 2. if cannot be found or args not match, etc. try reflection instead of
> failing.
>
> Thoughts?
>