For example I have handler:
@Component
public class MyHandler {
@AutoWired
private MyDependency myDependency;
public int someMethod() {
...
retur
All answers above are really good and may be useful so make sure you study and understand these principes first before continue reading my post.
In my scenario none of advices above did not work. I will post what helped me after a pretty long debugging.
If you want to call methods from tested class, the @Spy
annotation is needed alongside @InjectMocks
(or Mockito.spy(XXX)
call or course)
The interesting part is, the order of these annotations does matter! The @Spy
annotation must precede @InjectMocks
annotation.
Will not work
...
@InjectMocks
@Spy
private TestedObject instance
...
Will work
...
@Spy
@InjectMocks
private TestedObject instance
...