While testing my Spring classes with EasyMock, I came to this below scenario:
My Spring configuration is taking the original DAO object configured by Spring component-scan
rather than my mock DAO object.
Please find my mock, AppContext and test class below:
ApplicationContxt-Test.xml
<context:annotation-config />
<context:component-scan base-package="com.test.testclasses"/>
<import resource="mockServices.xml" />
MockServices.xml
<bean class="org.easymock.EasyMock" factory-method="createMock"
id="codeDAO" primary="true" >
<constructor-arg value="com.test.testclasses.dao.MaintainCodeDAO" />
</bean>
JUnit class:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:ApplicationContxt-Test.xml")
public class MaintainCodeServiceImplTest {
@Autowired
private MaintainCodeDAO codeDAO;
@Autowired
private MaintainCodeService maintainCodeService;
@Before
public void setUp() {
EasyMock.reset(codeDAO);
}
@After
public void tearDown() {
EasyMock.reset(codeDAO);
}
@Test
public void shouldAutowireDependencies() {
assertNotNull(codeDAO);
assertNotNull(maintainCodeService);
}
@Test
public void getProcedureByCode_success() throws Exception {
MaintainCodeVO maintainCodeVO = new MaintainCodeVO();
EasyMock.expect(codeDAO.searchProcedureCode(isA(String.class))).andReturn(maintainCodeVO);
EasyMock.replay(codeDAO);
MaintainCodeBO maintainCodeResult = maintainCodeService.getProcedureByCode("test");
EasyMock.verify(codeDAO);
codeDAO.searchProcedureCode("test");
assertNotNull(maintainCodeResult);
EasyMock.reset(codeDAO);
}
}
Here I am mocking codeDAO
and while testing the service class, instead of the mock codeDAO
, the original DAO object is getting autowired and EasyMock.verify()
is throwing exception. Don't know whats the issue. Is there any problem with the above configuration?