title: Autowiring beans implementing same interface - how to autowire a particular dependency bean in a JUnit Test?
body: <p>I have a Spring 3.1/Java/Tomcat application. I have a service class that is as follows:</p>
  
  <pre><code>public class SomeServiceImpl implements SomeService {
  
    @Autowired
    public AnotherService anotherService;
  
    //  other code...
  </code></pre>
  
  <p>This uses another service class AnotherService which is autowired. Both these services classes are declared in a serviceContext.xml file.</p>
  
  <p>I am writing junit to test SomeServiceImpl and use autowired to inject the class under test (SomeService) as well as the mock (EasyMock) dependency required by class under test (AnotherService). The easymock dependency for AnotherService is defined in a testContext.xml like below:</p>
  
  <pre><code>    &lt;bean id="mockAnotherService" class="org.easymock.EasyMock" factory-method="createMock" primary="true"&gt;
          &lt;constructor-arg value="com.xyz.AnotherService" /&gt;
      &lt;/bean&gt;
  </code></pre>
  
  <p>In my test class I configure to use both the context files. However, I am seeing that the ServiceImpl is correctly wired to the actual implementation (this is desired) but instead of mock version of AnotherService the concreate version (AnotherServiceImpl) is getting wired. </p>
  
  <p>How do I get the mocked version of my dependency wired instead? I can not use @Resource or @Qualifier in the actual implementation as that would defeat the purpose. I am ok to use these in my test class.</p>