I have a Spring 3.1/Java/Tomcat application. I have a service class that is as follows:
public class SomeServiceImpl implements SomeService {
@Autowired
public AnotherService anotherService;
// other code...
This uses another service class AnotherService which is autowired. Both these services classes are declared in a serviceContext.xml file.
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:
<bean id="mockAnotherService" class="org.easymock.EasyMock" factory-method="createMock" primary="true">
<constructor-arg value="com.xyz.AnotherService" />
</bean>
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.
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.