vendredi 3 juin 2016

EasyMock - Spring taking the original object not the mocked object

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. Dont know whats the issue. Is there any problem with the above configuration?

Aucun commentaire:

Enregistrer un commentaire