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"
      <constructor-arg index="0"
            value="com.argus.camc.datamanagement.interfaces.AccountDAO" />

Happy unit testing!

Leave a comment

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.