Closed GoogleCodeExporter closed 8 years ago
Hi,
That is expected with the current code, as the javadoc specify : "mocks will
first be resolved by type, then, if there is several property of the same type,
by the match of the field name and the mock name"
http://docs.mockito.googlecode.com/hg/latest/org/mockito/InjectMocks.html
We could enhance the autoinjection mechanism to work for these cases as well,
but it's not our priority.
Cheers,
Brice
Original comment by brice.du...@gmail.com
on 23 Aug 2012 at 4:09
Original comment by brice.du...@gmail.com
on 23 Aug 2012 at 4:09
[deleted comment]
Hi Brice,
I've read and re-read the documentation, and it states that it will try
constructor injection, followed by property setter injection, followed by field
injection. I don't have a matching constructor, so it should drop through to
property setter injection, since I have matching setters.
Please note that although it is doing some wiring by setter method, it is
wiring the mocks using the wrong setters, e.g. in the first screen shot, it has
wired the mock field called 'fileWriter' of type FileWriter using the
setPrintWriter(Writer) method rather than using the setFileWriter(Writer)
method.
This seems to me a clear contradiction of point 2 (Property Setter Injection)
in the JavaDoc: "mocks will first be resolved by type, then, if there is
several property of the same type, by the match of the property name and the
mock name." I get the same behaviour even if I explicitly set the mock name,
i.e. @Mock(name="fileWriter").
Thanks,
Al.
Original comment by alastair...@gmail.com
on 24 Aug 2012 at 9:16
Hi,
Let's forget about constructor injection, as it is not what is interesting us.
I think the issue is located in the javadoc, as it indicates how the algorithm
proceeds when injecting through a field or a setter. What this phrase meant to
say is that, mockito will look by type, if the mock type matches, then it will
be injected regardless of the name.
Anyway I don't mean that you are wrong, but rather that this is something to
enhance. But mockito is a mocking framework not a dependency injection one, so
we are not focusing the effort right now. Though it might be worth to mention
this current shortcoming !
Original comment by brice.du...@gmail.com
on 24 Aug 2012 at 9:35
Original comment by brice.du...@gmail.com
on 3 Sep 2012 at 9:59
Original comment by brice.du...@gmail.com
on 9 Jan 2013 at 2:47
I have the same issue with field injection and I would prefer if Mockito tries
to inject by name first.
Original comment by blys...@gmail.com
on 29 Jul 2013 at 4:02
This behavior won't change. Because many people don't name their mock to match
the exact property. It's safer to look at the type first.
And doing so might break exiting tests. I'm not sure this worth it.
Original comment by brice.du...@gmail.com
on 24 Jul 2014 at 7:03
Original comment by brice.du...@gmail.com
on 24 Jul 2014 at 7:03
Javadoc fixes here :
https://github.com/mockito/mockito/commit/f4f17245a28a82450725257c6c433dfc721af1
5b
Original comment by brice.du...@gmail.com
on 24 Jul 2014 at 7:06
Original comment by szcze...@gmail.com
on 16 Aug 2014 at 2:43
Original comment by szcze...@gmail.com
on 24 Aug 2014 at 3:14
Original comment by szcze...@gmail.com
on 24 Aug 2014 at 3:50
Original issue reported on code.google.com by
alastair...@gmail.com
on 23 Aug 2012 at 1:01Attachments: