title: Spring / Mock components that are retrieved by using the context directly
body: <p>We have a project that uses <code>Spring</code> annotations to configure its context.
  To test this project we are using <code>Mockito</code> and it's <code>Spring</code> extension.</p>
  
  <p>In tests I need to override some beans with mock/spy version.</p>
  
  <p>With the <code>@Mock</code>/<code>@Spy</code> and <code>@InjectMock</code> annotations I have been able to use spy for beans using autowiring mechanism.</p>
  
  <p>Now I have a third party component which create another Spring context and then merge the 2 contexts together. This third party component retrieve beans using a call to the context:</p>
  
  <pre><code>applicationContext.getBean(key);
  </code></pre>
  
  <p>In this case, the @Mock/@Spy and @InjectMock combination is not working.</p>
  
  <p>The 'workaround' solution I have put in place is an XML file in which I declare my spied bean:</p>
  
  <pre><code>&lt;mockito:spy beanName="beanToSpy"/&gt;
  </code></pre>
  
  <p>To stay in the annotation world, I have tried <a href="https://bitbucket.org/kubek2k/springockito/wiki/springockito-annotations" rel="nofollow noreferrer">springockito-annotations</a> as mentioned in these similar questions:
  <a href="https://stackoverflow.com/questions/2457239/injecting-mockito-mocks-into-a-spring-bean">Injecting Mockito mocks into a Spring bean</a>
  and its duplicate:
  <a href="https://stackoverflow.com/questions/19808326/how-to-inject-a-mock-in-a-spring-context">How to inject a Mock in a Spring Context</a></p>
  
  <p>But the bean is not spied or mocked, I've probably a configuration error.  </p>
  
  <p>My current setup is:</p>
  
  <p>A base class that is in charge of the Spring config for test:</p>
  
  <pre><code>@RunWith(SpringJUnit4ClassRunner.class)
  @ActiveProfiles("test")
  @ContextConfiguration(loader= SpringockitoContextLoader.class, locations ={"/config.xml","/test-config.xml"})
  public abstract class BaseTest {
    //...
  }
  </code></pre>
  
  <p>A test class that would like to use a mocked bean:</p>
  
  <pre><code>public class MyTest extends BaseTest {
    @ReplaceWithMock @Autowired MyService myService;
    @WrapWithSpy @Autowired OtherService otherService;
  
    @Test public void someTest() { 
      doReturn(x).when(myService).call();
      doReturn(y).when(otherService).process();
    }
  }
  </code></pre>
  
  <p>Unfortunately in <code>MyTest</code>, the beans are not replaced by their <code>mock</code>/<code>spy</code> versions, it is the plain old regular version.</p>
  
  <p>EDIT: 
  The third party component that does the lookup is using its own spring parent context and add the current spring context into its own. The lookup to retrieve the component that I want to be mocked occurs after the context has been fully loaded.</p>
  
  <p>What should I do to properly replace the bean in the context with a <code>mock</code>/<code>spy</code> version ? 
  What is wrong with the way I'm using <code>@WrapWithSpy</code> / <code>@ReplaceWithMock</code> ?</p>