The simplest solution is load all your xml. And manually replace dependence in test case.
@ContextConfiguration("logicContext.xml")
public class BaseLogicTest extends AbstractTestNGSpringContextTests {
@Autowired
private MyLogic myLogic;
@Before
public void injectTestDoubles() {
myLogic.setMyDao(...);
}
@DirtiesContext
@Test ...//test methods
}
But this corrupts the application context, so you need @DirtiesContext if you need "real" myDao in other test case sharing the same application context.
The most popular solution (my personal opinion :P ) is using mockito and a test-specific xml.
//daoContext.xml
<bean id="myDao" class="com.MyDao"/>
//logicContext.xml
<bean id="myLogic" class="com.MyLogic">
<constructor-arg ref="myDao"/><!--or other type of injection--?
</bean>
//test-logicContext.xml
<bean id="myDao" class="org.mockito.Mockito" factory-method="mock">
<constructor-arg value="com.MyDao" />
</bean>
@ContextConfiguration({"logicContext.xml", "test-logicContext.xml"})
public class BaseLogicTest extends AbstractTestNGSpringContextTests {
@Autowired
private MyLogic myLogic;
@Autowired
private MyDao myDao;//retrieve mock so you could define expectations
@Test //test methods
@After public void clearMocks() {
Mockito.reset(myDao);//this is important if you have several test methods
}
}
And this solution works for other mock framework like easyMock.