Closed GoogleCodeExporter closed 8 years ago
Hi,
Your code is incorrect in several ways:
- the any() matcher does not check for types, this wasn't clear in the javadoc, though it has been updated. http://docs.mockito.googlecode.com/hg/1.9.0/org/mockito/Matchers.html
You should use instead the isA() matcher.
- your mocking is incorrect, as the any() matchers do not uses the type, which means that the last stub replaces the first. i.e. service.findByExample will always return a User.
So you should use instead isA() matcher and it will work as expected.
Alternatively if you want to stub consecutive calls you should use the
documented way :
http://docs.mockito.googlecode.com/hg/1.9.0/org/mockito/Mockito.html#10
Or with your syntax style :
doReturn(company).doReturn(user).when(service).findByExample(any());
- You should avoid using both @Spy and @InjectMocks on the same class, it's possible but I would advise against as it means you have some flaws in your design to require to both spy and inject mocks in the tested object.
Original comment by brice.du...@gmail.com
on 20 Aug 2012 at 2:20
very thanks, Brice.
Original comment by xpj...@gmail.com
on 24 Aug 2012 at 5:37
you are welcome :)
Cheers,
Brice
Original comment by brice.du...@gmail.com
on 24 Aug 2012 at 8:36
Original comment by brice.du...@gmail.com
on 3 Sep 2012 at 10:00
Original issue reported on code.google.com by
xpj...@gmail.com
on 10 Aug 2012 at 5:45