<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Code Hangover &#187; test</title>
	<atom:link href="http://blog.codehangover.com/tag/test/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.codehangover.com</link>
	<description>Go ahead, have another</description>
	<lastBuildDate>Tue, 22 Mar 2011 15:49:48 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Getting friendly with Spring, JUnit and EasyMock.</title>
		<link>http://blog.codehangover.com/getting-friendly-with-spring-junit-and-easymock/</link>
		<comments>http://blog.codehangover.com/getting-friendly-with-spring-junit-and-easymock/#comments</comments>
		<pubDate>Sun, 02 Aug 2009 00:31:43 +0000</pubDate>
		<dc:creator>DanEngland</dc:creator>
				<category><![CDATA[java]]></category>
		<category><![CDATA[easymock]]></category>
		<category><![CDATA[spring]]></category>
		<category><![CDATA[test]]></category>

		<guid isPermaLink="false">http://blog.codehangover.com/?p=240</guid>
		<description><![CDATA[Here are some steps that can get you using Spring, JUnit and EasyMock all together in some Test Driven Development hotness.]]></description>
			<content:encoded><![CDATA[<script type="text/javascript">dzone_url = "http://blog.codehangover.com/getting-friendly-with-spring-junit-and-easymock/";</script><p>Here are some steps that can get you using Spring, JUnit and EasyMock all together in some Test Driven Development hotness.</p>
<p>Start by adding the following lines to the top of your unit test.  Specifying Autowire by name ensures you get the injection you want and will stop those Spring errors that there are more than one of the same type of mock objects in your <tt>mock-applicationContext.xml</tt>.  When you specify the Spring Junit runner you must provide one ore more context configurations with @ContextConfiguration.</p>
<p><strong>MyClassUnitTest.java</strong></p>
<pre class="brush: java;">
...
@Configurable(autowire = Autowire.BY_NAME)
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {&quot;classpath:com/some/domain/someproject/resources/mock-applicationContext.xml&quot;})
public class MyClassUnitTest {
...
     @Autowired private Collaborator mockCollaborator;

     @Before
     public void setup() throws Exception {
     ...
     }

     @After
     public void teardown() throws Exception {
     ...
     }

     @Test
     public void testMyClass() throws Exception {

          //Set your mock behavior here
          ....

          EasyMock.replay(mockCollaborator);

          MyClass myClass = new MyClass(mockCollaborator);
          myClass.run();
          EasyMock.verify(mockCollaborator);

          // Other JUnit assertions
          ...
     }
 }
</pre>
<p>Then add your mocks to your <tt>mock-applicationContext.xml</tt> (an applicationContext in your test resources just for providing your unit tests with spring injection dependencies):</p>
<p><strong>mock-applicationContext.xml</strong></p>
<pre class="brush: xml;">
...
&lt;bean id=&quot;mockCollaborator&quot; name=&quot;mockCollaborator&quot; class=&quot;org.easymock.EasyMock&quot; factory-method=&quot;createStrictMock&quot;&gt;
     &lt;constructor-arg value=&quot;com.some.domain.someproject.Collaborator&quot;/&gt;
&lt;/bean&gt;

&lt;bean id=&quot;mockOtherCollaborator&quot; name=&quot;mockOtherCollaborator&quot; class=&quot;org.easymock.EasyMock&quot; factory-method=&quot;createStrictMock&quot;&gt;
     &lt;constructor-arg value=&quot;com.some.domain.someproject.OtherCollaborator&quot;/&gt;
&lt;/bean&gt;
...
</pre>
<p>Ensure your maven pom has the following test-scoped dependency:</p>
<p><strong>pom.xml</strong></p>
<pre class="brush: xml;">
...
&lt;dependency&gt;
     &lt;groupId&gt;org.springframework&lt;/groupId&gt;
     &lt;artifactId&gt;spring-test&lt;/artifactId&gt;
     &lt;version&gt;2.5.6&lt;/version&gt;
     &lt;scope&gt;test&lt;/scope&gt;
&lt;/dependency&gt;
...
</pre>
<p>Then use your mocks as normal. Make sure you call <tt>EasyMock.reset(mock)</tt> in your @After <tt>teardown()</tt> method so that each of your mocks are reset for each test.  You can also tell Spring to reset the context after a test if needed with @DirtiesContext.</p>
<p>For official documentation and tutorials check out these links.</p>
<h4>Spring</h4>
<p><a href="http://www.springsource.org/documentation">http://www.springsource.org/documentation</a></p>
<h4>JUnit</h4>
<p><a href="http://www.springsource.org/documentation">http://www.junit.org</a></p>
<h4>EasyMock</h4>
<p><a href="http://easymock.org/Documentation.html">http://easymock.org/Documentation.html</a><br />
<h3>Related Posts</h3>
<ul class="related_post">
<li><a href="http://blog.codehangover.com/spring-patterns-best-practices-and-design-strategies-book-review/" title="Spring Patterns: Best Practices and Design Strategies Book Review">Spring Patterns: Best Practices and Design Strategies Book Review</a></li>
<li><a href="http://blog.codehangover.com/load-multiple-contexts-into-spring/" title="Load Multiple Contexts into Spring">Load Multiple Contexts into Spring</a></li>
<li><a href="http://blog.codehangover.com/choose-many-spring-contexts-over-a-single-context/" title="Choose many Spring contexts over a single context">Choose many Spring contexts over a single context</a></li>
</ul>
<script>var dzone_style="2";</script><script language="javascript" src="http://widgets.dzone.com/widgets/zoneit.js"></script><a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save?linkurl=http%3A%2F%2Fblog.codehangover.com%2Fgetting-friendly-with-spring-junit-and-easymock%2F&amp;linkname=Getting%20friendly%20with%20Spring%2C%20JUnit%20and%20EasyMock."><img src="http://blog.codehangover.com/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share/Bookmark"/></a>]]></content:encoded>
			<wfw:commentRss>http://blog.codehangover.com/getting-friendly-with-spring-junit-and-easymock/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

