I have a Service Class with 3 methods, Service class is also using some @Autowired annotations. Out of 3 methods, I want to mock two methods but use real method for 3rd one.
I was surprised myself but it does work for us. We have plenty places like:
@Spy
@Autowired
private FeatureService featureService;
I think I know why you are facing this problem. It's not about injection, it's about when(bloMock.doSomeStuff()).thenReturn(1)
vs doReturn(1).when(bloMock).doSomeStuff()
.
See: http://www.stevenschwenke.de/spyingWithMockito
The very important difference is that the first option will actually call the doSomeStuff()- method while the second will not. Both will cause doSomeStuff() to return the desired 1.