Spring Tips: Initializing bean using EasyMock for unit test

Unit test is a very good practice for creating robust application. Using Spring, we can avoid using ApplicationContext and instead arrange all dependencies programmatically. However, once in a while, we still need to test using the actual ApplicationContext. This can be problematic if our test using mock from EasyMock.

So here is the example on how to create a mock using EasyMock programmatically:

AccountDAO accountDAOMock = EasyMock.createMock(AccountDAO.class);

Move it to Spring ApplicationContext like this:

<bean id="accountDAOMock" class="org.easymock.EasyMock"
		factory-method="createMock">
      <constructor-arg index="0"
            value="com.argus.camc.datamanagement.interfaces.AccountDAO" />
</bean>

Happy unit testing!

0 Responses to “Spring Tips: Initializing bean using EasyMock for unit test”


  • No Comments

Leave a Reply