Mockito inject nested bean
<p>Im not new to mockito, but this time I found an interesting case during my work. I hope you can help me out with it. </p>

<p>I need to inject mock to change certain method behaviour during the test. The problem is, the bean structure is nested, and this bean is inside other beans, not accessible from test method. My code looks like this:</p>

<pre><code>@Component
class TestedService { 
  @Autowired
  NestedService nestedService;
}

@Component
class NestedService {
  @Autowired
  MoreNestedService moreNestedService;
}

@Component
class MoreNestedService {
  @Autowired
  NestedDao nestedDao;
}

@Component
class NestedDao {
  public int method(){
    //typical dao method, details omitted
  };
}
</code></pre>

<p>So in my test I would like the call NestedDao.method to return mocked answer. </p>

<pre><code>class Test { 
  @Mock
  NestedDao nestedDao;

  @InjectMocks
  TestedService testedSevice;

  @Test
  void test() {
    Mockito.when(nestedDao.method()).thenReturn(1);
    //data preparation omitted
    testedSevice.callNestedServiceThatCallsNestedDaoMethod();
    //assertions omitted
  }
}
</code></pre>

<p>I have tried to do a initMocks:</p>

<pre><code>@BeforeMethod
  public void setUp() throws Exception {
    MockitoAnnotations.initMocks(this);
}
</code></pre>

<p>Also to add annotation upon my test class:</p>

<pre><code>@RunWith(MockitoJUnitRunner.class)
</code></pre>

<p>Always getting nullpointers or wrong answer from method (not mocked). </p>

<p>I guess it's this nested calls fault, making it impossible to mock this Dao. 
Also Ive read that @InjectMocks only would work with setters or constructor injection, which im missing (just @Autowire on private fields), but it didn't worked when I tried. </p>

<p>Any guess what am I missing? ;)</p>