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:

<br />
AccountDAO accountDAOMock = EasyMock.createMock(AccountDAO.class);<br />

Move it to Spring ApplicationContext like this:

<br />
&lt;bean id=&quot;accountDAOMock&quot; class=&quot;org.easymock.EasyMock&quot;<br />
		factory-method=&quot;createMock&quot;&gt;<br />
      &lt;constructor-arg index=&quot;0&quot;<br />
            value=&quot;com.argus.camc.datamanagement.interfaces.AccountDAO&quot; /&gt;<br />
&lt;/bean&gt;<br />

Happy unit testing!

Leave a Reply

Your email address will not be published. Required fields are marked *

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