this line got my attention
@InjectMocks private ClassToBeInjected injectedObj =
mock(ClassToBeInjected.class);
shouldn't you rather use this annotation on your SUT so it get the
mocks injected?
http://docs.mockito.googlecode.com/hg/org/mockito/InjectMocks.html
--
Regards / Pozdrawiam
Tomek Kaczanowski
http://practicalunittesting.com
2013/1/10 Jumwah <
james.mil...@gmail.com>:
> Hello,
>
> I am getting some odd injection behavior when I was using Mockito 1.8.5, and
> when I recently upgraded to 1.9.5 it got worse and was causing a test to
> fail. I don't have much time to create a example right now, so I'll try to
> explain to see if anyone can see a rookie mistake.
>
> I was using injection like so:
>
> @Mock(name = "mousePosition") private Point2D mousePosition;
>
> @InjectMocks private ClassToBeInjected injectedObj =
> mock(ClassToBeInjected.class);
>
> Pretty straightforward. The ClassToBeInjected has several base classes and
> the field "mousePosition" belongs to one of the base classes. The class
> that owns it also has a field called "menuPosition" (also of Point2D type),
> and an even more base class has fields called "leftDragStart" and
> "leftDragStop" (also of Point2D type). All these fields are protected - if
> that makes a difference.
>
> In 1.8.5, the mock for mousePosition would be assigned to ALL of these
> fields! When I upgraded to 1.9.5, the mock for mousePosition was being
> assigned to menuPosition and leftDragStart. This caused my test to fail as
> the actual field in the ClassToBeInjected was left as null.
>
> In order to fix this, I had to add:
>
> @Mock(name = "menuPosition") private Point2D menuPosition;
>
> which interestingly enough, also stopped the mousePosition mock being
> assigned to the leftDragStart field! The most crazy part? A field called
> mouseListener (owned by Component which is a base class of
> ClassToBeInjected) is being assigned the ClassToBeInjected mock itself!! So
> it was injecting itself with... itself.
>
> This behaviour has been confirmed on two colleagues machines, so I don't
> have a build problem. This feels utterly bonkers to me - can anyone see why
> I would be getting this behviour?
>
> Thanks!
>
> --
> You received this message because you are subscribed to the Google Groups
> "mockito" group.
> To view this discussion on the web visit
>
https://groups.google.com/d/msg/mockito/-/g27o4NIyoJ4J.
> To post to this group, send email to
moc...@googlegroups.com.
> To unsubscribe from this group, send email to
>
mockito+u...@googlegroups.com.
> For more options, visit this group at
>
http://groups.google.com/group/mockito?hl=en.