0

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?

dur
  • 15,689
  • 25
  • 79
  • 125
Arun
  • 3,701
  • 5
  • 32
  • 43

3 Answers3

2

I found the solution. The trick was so simple .. I killed one day finding the solution.. So here is the solution:

Just move the mockServices.xml (spring xml with only mock configurations) above context:component-scan...

    <import resource="mockServices.xml" />
    <context:annotation-config />
    <context:component-scan base-package="com.wellpoint.icnotes"/>

Worked like a charm.

Arun
  • 3,701
  • 5
  • 32
  • 43
-1

If I remember correctly, because of createMock is a generic method, the Spring doesn't work properly while inferring the bean type, it infers it as an Object, so it cannot wire. I suggest not to use the Spring in unit tests, use simple classes.

BTW, EasyMock has it's own DI, just use it: http://easymock.org/user-guide.html#mocking-annotations

It is possible to put mocks into context, e.g. as described here: Autowiring of beans generated by EasyMock factory-method? but for me it is not the best approach, looks clumsy.

Community
  • 1
  • 1
kan
  • 28,279
  • 7
  • 71
  • 101
  • Yea we can do that..As u said it looks too clumsy. I need to write unit test cases for more than 100 service classes and this doesn't seem to be a good idea. I have done this before and don'y know why it is not working now..... – Arun Jun 06 '16 at 06:48
  • @ArunRaj I prefer injections by EasyMock/Mockito DI. No spring - works faster, looks better, less dependencies. – kan Jun 06 '16 at 09:11
-1

If you are using Spring Framework 4.2 or higher, this should work. See the following related issues for details.

Community
  • 1
  • 1
Sam Brannen
  • 29,611
  • 5
  • 104
  • 136