title: Powemock mock inner @Autowired dependency of class
body: <p>I have <code>@Autowired</code> dependency in test that have another <code>@Authowired</code> dependency in it. I'm using</p>
  
  <pre><code>@RunWith(SpringJUnit4ClassRunner.class) 
  </code></pre>
  
  <p>and </p>
  
  <pre><code>@Rule
  public PowerMockRule rule = new PowerMockRule();
  </code></pre>
  
  <p>in order to get spring context in my test.</p>
  
  <p>I need to mock the inner dependency of <code>@Autowired</code> member. I can do this by writing following code:</p>
  
  <pre><code>@Autowired
  private CartService cartService;
  
  private MyValidator validator;
  
  @Test
  public void addItemToCartTest() {
  
   validator =  PowerMock.createMock(MyValidator.class);
   expect(validator.validate(item)).andReturn(true);
   cartService.setValidator(validator);
   PowerMock.replayAll();
  
   cartService.addItem(item);
       //some code
   verifyAll();
  }
  </code></pre>
  
  <p>Everything works this way but I need to explicitly call set method of the cartService. I added this method just for test and now I want to remove it but if I'm not setting the validator, it not being mocked.</p>
  
  <p>How can I mock it anyway?</p>