<?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>maxheapsize.com &#187; spring</title>
	<atom:link href="http://maxheapsize.com/tag/spring/feed/" rel="self" type="application/rss+xml" />
	<link>http://maxheapsize.com</link>
	<description>Oliver Wehrens on Programming and Agile</description>
	<lastBuildDate>Tue, 14 Jun 2011 20:41:41 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1.2</generator>
		<item>
		<title>How to test Spring Webflow 2 (with parent flows)</title>
		<link>http://maxheapsize.com/2009/04/20/how-to-test-spring-webflow-2-with-parent-flows/</link>
		<comments>http://maxheapsize.com/2009/04/20/how-to-test-spring-webflow-2-with-parent-flows/#comments</comments>
		<pubDate>Mon, 20 Apr 2009 18:32:48 +0000</pubDate>
		<dc:creator>Oliver Wehrens</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[junit]]></category>
		<category><![CDATA[spring]]></category>
		<category><![CDATA[test]]></category>
		<category><![CDATA[Web]]></category>
		<category><![CDATA[webflow]]></category>

		<guid isPermaLink="false">http://maxheapsize.com/?p=210</guid>
		<description><![CDATA[In the last couple of month I was writing some flows using Spring Webflow 2. I knew that I (in theory) I could test them but I never did. The overall documentation of Webflow 2 is not that great at the moment, so I hope to improve that a little bit. Spring Webflow 2 supports [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fmaxheapsize.com%2F2009%2F04%2F20%2Fhow-to-test-spring-webflow-2-with-parent-flows%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fmaxheapsize.com%2F2009%2F04%2F20%2Fhow-to-test-spring-webflow-2-with-parent-flows%2F&amp;source=owehrens&amp;style=normal&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p>In the last couple of month I was writing some flows using Spring Webflow 2. I knew that I (in theory) I could test them but I never did. The overall documentation of Webflow 2 is not that great at the moment, so I hope to improve that a little bit.</p>
<p>Spring Webflow 2 supports only JUnit tests so far. I guess this will change at some point  but until then you have to pull out that junit.jar again. </p>
<p>Sorry for the awkward line breaks. Spring just does love long method and class names (which is not that bad).</p>
<p>Your Testclass needs to extends <code>AbstractXmlFlowExecutionTests</code> and to implement<code> protected FlowDefinitionResource getResource(FlowDefinitionResourceFactory resourceFactory)</code>. </p>
<pre class="brush: java; title: ;">
public class MyWebflowTest extends AbstractXmlFlowExecutionTests
  protected FlowDefinitionResource getResource(
     FlowDefinitionResourceFactory resourceFactory) {
    FlowDefinitionResource flowDefinitionResource =
      resourceFactory.createResource(&quot;flows/administration/skinManagement.flow.xml&quot;);
    return flowDefinitionResource;
  }
</pre>
<p>If your flow now happen to have a parent flow you are in a bit of trouble. You just can include one flow in the <code>FlowDefinitionResource</code>. The parent tag in your flow points to an id of the parentflow which is usually defined in a webflow configuration file. If this parent flow again has parents as well&#8230;yes, same game again.</p>
<p>To get a hold of these flows you need to override <code>getModelResources[]</code>.</p>
<pre class="brush: java; title: ;">
@Override
protected FlowDefinitionResource[]
 getModelResources(FlowDefinitionResourceFactory resourceFactory) {
  FlowDefinitionResource[] flowDefinitionResources =
 new FlowDefinitionResource[3];
  flowDefinitionResources[0] =
    resourceFactory.createResource(&quot;flows/common.flow.xml&quot;);
  flowDefinitionResources[1] =
    resourceFactory.createResource(&quot;flows/common-exceptionHandling.flow.xml&quot;);
  flowDefinitionResources[2] =
    resourceFactory.createResource(&quot;flows/common-menu.flow.xml&quot;);
  return flowDefinitionResources;
}
</pre>
<p>If one of your flows is using beans you want to mock them in the test.  You can do that by registering with the <code>MockFlowBuildContext</code>. The method <code>configureFlowBuilderContext</code> will be called by <code>AbstractXmlFlowExecutionTests</code> </p>
<pre class="brush: java; title: ;">
@Override
protected void configureFlowBuilderContext(
    MockFlowBuilderContext builderContext) {
  builderContext.registerBean(&quot;validationExceptionHandler&quot;,
    new ValidationExceptionHandler());
  builderContext.registerBean(&quot;infastructureExceptionHandler&quot;,
    new InfastructureExceptionHandler());
  builderContext.registerBean(&quot;springSecurityExceptionHandler&quot;,
    new SpringSecurityExceptionHandler());
}
</pre>
<p>With this setup you now can start testing the flow.</p>
<pre class="brush: java; title: ;">
...
public void testSkinManagementView() {
  MockExternalContext mockExternalContext = new MockExternalContext();
  startFlow(mockExternalContext);
  assertFlowExecutionActive();
  assertCurrentStateEquals(&quot;showSkinManagementOverview&quot;);
}
...
</pre>
<p>I think the code is pretty self explanatory.</p>
<p>You have several possibilities including start and resume flow and assert certain states. You can test action methods doing something (like calling a service) and making sure the correct flow is executed. </p>
<p>Figuring out the initial setup with the parent flow and beans was the most time consuming task. You need to make sure that you only put flow logic in the definition of the flow since that is what webflow is for. I think with some clever usage of <code>set</code> and <code>result</code> you can make things complicated and a bit tricky.</p>
<div class="google_plusone_widget"><g:plusone 
      count="true" href="http://maxheapsize.com/2009/04/20/how-to-test-spring-webflow-2-with-parent-flows/" size="medium"></g:plusone></div><!-- Social Bookmarks BEGIN -->
<div class="social_bookmark">
<br />
<div class="d">
<br />
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://del.icio.us/post?url=http://maxheapsize.com/2009/04/20/how-to-test-spring-webflow-2-with-parent-flows/&amp;title=How+to+test+Spring+Webflow+2+%28with+parent+flows%29" rel="nofollow" title="Add to&nbsp;Del.icio.us"><img class="social_img" src="http://maxheapsize.com/wp-content/plugins/social-bookmarks/images/delicious.png" title="Add to&nbsp;Del.icio.us" alt="Add to&nbsp;Del.icio.us" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://digg.com/submit?phase=2&amp;url=http://maxheapsize.com/2009/04/20/how-to-test-spring-webflow-2-with-parent-flows/&amp;title=How+to+test+Spring+Webflow+2+%28with+parent+flows%29" rel="nofollow" title="Add to&nbsp;digg"><img class="social_img" src="http://maxheapsize.com/wp-content/plugins/social-bookmarks/images/digg.png" title="Add to&nbsp;digg" alt="Add to&nbsp;digg" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.dzone.com/links/add.html?description=How+to+test+Spring+Webflow+2+%28with+parent+flows%29&amp;url=http://maxheapsize.com/2009/04/20/how-to-test-spring-webflow-2-with-parent-flows/&amp;title=How+to+test+Spring+Webflow+2+%28with+parent+flows%29" rel="nofollow" title="Add to&nbsp;DZone"><img class="social_img" src="http://maxheapsize.com/wp-content/plugins/social-bookmarks/images/dzone.png" title="Add to&nbsp;DZone" alt="Add to&nbsp;DZone" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.facebook.com/sharer.php?u=http://maxheapsize.com/2009/04/20/how-to-test-spring-webflow-2-with-parent-flows/" rel="nofollow" title="Add to&nbsp;Facebook"><img class="social_img" src="http://maxheapsize.com/wp-content/plugins/social-bookmarks/images/facebook.png" title="Add to&nbsp;Facebook" alt="Add to&nbsp;Facebook" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.google.com/bookmarks/mark?op=edit&amp;output=popup&amp;bkmk=http://maxheapsize.com/2009/04/20/how-to-test-spring-webflow-2-with-parent-flows/&amp;title=How+to+test+Spring+Webflow+2+%28with+parent+flows%29" rel="nofollow" title="Add to&nbsp;Google Bookmarks"><img class="social_img" src="http://maxheapsize.com/wp-content/plugins/social-bookmarks/images/google.png" title="Add to&nbsp;Google Bookmarks" alt="Add to&nbsp;Google Bookmarks" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.mister-wong.com/index.php?action=addurl&amp;bm_url=http://maxheapsize.com/2009/04/20/how-to-test-spring-webflow-2-with-parent-flows/&amp;bm_description=How+to+test+Spring+Webflow+2+%28with+parent+flows%29" rel="nofollow" title="Add to&nbsp;Mister Wong"><img class="social_img" src="http://maxheapsize.com/wp-content/plugins/social-bookmarks/images/misterwong.png" title="Add to&nbsp;Mister Wong" alt="Add to&nbsp;Mister Wong" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://reddit.com/submit?url=http://maxheapsize.com/2009/04/20/how-to-test-spring-webflow-2-with-parent-flows/&amp;title=How+to+test+Spring+Webflow+2+%28with+parent+flows%29" rel="nofollow" title="Add to&nbsp;reddit"><img class="social_img" src="http://maxheapsize.com/wp-content/plugins/social-bookmarks/images/reddit.png" title="Add to&nbsp;reddit" alt="Add to&nbsp;reddit" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.stumbleupon.com/submit.php?url=http://maxheapsize.com/2009/04/20/how-to-test-spring-webflow-2-with-parent-flows/&amp;title=How+to+test+Spring+Webflow+2+%28with+parent+flows%29" rel="nofollow" title="Add to&nbsp;Stumble Upon"><img class="social_img" src="http://maxheapsize.com/wp-content/plugins/social-bookmarks/images/stumbleupon.png" title="Add to&nbsp;Stumble Upon" alt="Add to&nbsp;Stumble Upon" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.sphere.com/sphereit/http://maxheapsize.com/2009/04/20/how-to-test-spring-webflow-2-with-parent-flows/" rel="nofollow" title="Add to&nbsp;SphereIt"><img class="social_img" src="http://maxheapsize.com/wp-content/plugins/social-bookmarks/images/sphereit.png" title="Add to&nbsp;SphereIt" alt="Add to&nbsp;SphereIt" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.spurl.net/spurl.php?url=http://maxheapsize.com/2009/04/20/how-to-test-spring-webflow-2-with-parent-flows/&amp;title=How+to+test+Spring+Webflow+2+%28with+parent+flows%29" rel="nofollow" title="Add to&nbsp;Spurl"><img class="social_img" src="http://maxheapsize.com/wp-content/plugins/social-bookmarks/images/spurl.png" title="Add to&nbsp;Spurl" alt="Add to&nbsp;Spurl" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.technorati.com/faves?add=http://maxheapsize.com/2009/04/20/how-to-test-spring-webflow-2-with-parent-flows/" rel="nofollow" title="Add to&nbsp;Technorati"><img class="social_img" src="http://maxheapsize.com/wp-content/plugins/social-bookmarks/images/technorati.png" title="Add to&nbsp;Technorati" alt="Add to&nbsp;Technorati" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://twitter.com/home/?status=Check+out+How+to+test+Spring+Webflow+2+%28with+parent+flows%29+@+http://maxheapsize.com/2009/04/20/how-to-test-spring-webflow-2-with-parent-flows/" rel="nofollow" title="Add to&nbsp;Twitter"><img class="social_img" src="http://maxheapsize.com/wp-content/plugins/social-bookmarks/images/twitter.png" title="Add to&nbsp;Twitter" alt="Add to&nbsp;Twitter" /></a>
<br />
</div>
</div>
<!-- Social Bookmarks END -->
]]></content:encoded>
			<wfw:commentRss>http://maxheapsize.com/2009/04/20/how-to-test-spring-webflow-2-with-parent-flows/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>How to refactor Spring Webflow variables in your JSF pages with IntelliJ IDEA</title>
		<link>http://maxheapsize.com/2009/03/18/how-to-refactor-spring-webflow-variables-in-your-jsf-pages-with-intellij-idea/</link>
		<comments>http://maxheapsize.com/2009/03/18/how-to-refactor-spring-webflow-variables-in-your-jsf-pages-with-intellij-idea/#comments</comments>
		<pubDate>Wed, 18 Mar 2009 19:40:29 +0000</pubDate>
		<dc:creator>Oliver Wehrens</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[intellij idea]]></category>
		<category><![CDATA[jsf]]></category>
		<category><![CDATA[refactor]]></category>
		<category><![CDATA[spring]]></category>
		<category><![CDATA[webflow]]></category>

		<guid isPermaLink="false">http://maxheapsize.com/?p=128</guid>
		<description><![CDATA[We are using JSF, Facelets and Spring Webflow in the product I&#8217;m currently working on. What bugged me for some time already was that when we started to refactor the domain and the corresponding dto&#8217;s the GUI was a problem since the variables which get pulled out of the webflow are just string declarations. I [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fmaxheapsize.com%2F2009%2F03%2F18%2Fhow-to-refactor-spring-webflow-variables-in-your-jsf-pages-with-intellij-idea%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fmaxheapsize.com%2F2009%2F03%2F18%2Fhow-to-refactor-spring-webflow-variables-in-your-jsf-pages-with-intellij-idea%2F&amp;source=owehrens&amp;style=normal&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p>We are using <strong>JSF,</strong> <strong>Facelets</strong> and <strong>Spring Webflow</strong> in the product I&#8217;m currently working on. What bugged me for some time already was that when we started to refactor the domain and the corresponding dto&#8217;s the GUI was a problem since the variables which get pulled out of the webflow are just string declarations. I had to go to each xhtml file and change the code to reflect the access to the new properties.</p>
<p>For some reason I missed a feature of IntelliJ IDEA completely.  </p>
<p>If you are using Spring Webflow your code might look like this:</p>
<pre class="brush: xml; title: ;">
&lt;tr:outputText value=&quot;#{flowScope.myViewBean.creditCardDto.fullName}&quot;/&gt;
</pre>
<p>You get no code completion nor refactoring security. </p>
<p>Now it is possible to make this a little bit <strong>better</strong>.</p>
<pre class="brush: xml; title: ;">
&lt;c:set value=&quot;#{flowScope.creditCardDto}&quot; var=&quot;creditCardDto&quot;&gt;&lt;/c:set&gt;
&lt;!--@elvariable id=&quot;creditCardDto&quot;
     type=&quot;net.wehrens.accounting.CreditCardDto&quot;--&gt;
&lt;tr:outputText value=&quot;#{creditCardDto.fullName}&quot;/&gt;
</pre>
<p>With the clever Inspection &#8216;<strong>Declare External Variable in Comment Annotation</strong>&#8216; (just press Alt+Enter on the usage of creditCardDto) IDEA creates a comment annotation which tells the IDE of what type the given variable is.  Since you are using webflow, you would pull the needed variable with a c:set tag out of the flowScope (or any other scope) and then declare the variable and it&#8217;s type. </p>
<p>There you go. </p>
<p>You can now start refactoring the creditCardDto and the GUI will reflect the changes. No more manual editing.</p>
<p>To make sure the comments do not get rendered out to the client (and with that the class information) you can turn that off for faclets in the web.xml  configuration.</p>
<pre class="brush: xml; title: ;">
&lt;context-param&gt;
    &lt;param-name&gt;facelets.SKIP_COMMENTS&lt;/param-name&gt;
    &lt;param-value&gt;true&lt;/param-value&gt;
&lt;/context-param&gt;
</pre>
<p>This is not specific to JSF, facelets or anything. This works with Freemaker, JSP&#8217;s and others as well.</p>
<p>My life just got a whole lot better.</p>
<div class="google_plusone_widget"><g:plusone 
      count="true" href="http://maxheapsize.com/2009/03/18/how-to-refactor-spring-webflow-variables-in-your-jsf-pages-with-intellij-idea/" size="medium"></g:plusone></div><!-- Social Bookmarks BEGIN -->
<div class="social_bookmark">
<br />
<div class="d">
<br />
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://del.icio.us/post?url=http://maxheapsize.com/2009/03/18/how-to-refactor-spring-webflow-variables-in-your-jsf-pages-with-intellij-idea/&amp;title=How+to+refactor+Spring+Webflow+variables+in+your+JSF+pages+with+IntelliJ+IDEA" rel="nofollow" title="Add to&nbsp;Del.icio.us"><img class="social_img" src="http://maxheapsize.com/wp-content/plugins/social-bookmarks/images/delicious.png" title="Add to&nbsp;Del.icio.us" alt="Add to&nbsp;Del.icio.us" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://digg.com/submit?phase=2&amp;url=http://maxheapsize.com/2009/03/18/how-to-refactor-spring-webflow-variables-in-your-jsf-pages-with-intellij-idea/&amp;title=How+to+refactor+Spring+Webflow+variables+in+your+JSF+pages+with+IntelliJ+IDEA" rel="nofollow" title="Add to&nbsp;digg"><img class="social_img" src="http://maxheapsize.com/wp-content/plugins/social-bookmarks/images/digg.png" title="Add to&nbsp;digg" alt="Add to&nbsp;digg" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.dzone.com/links/add.html?description=How+to+refactor+Spring+Webflow+variables+in+your+JSF+pages+with+IntelliJ+IDEA&amp;url=http://maxheapsize.com/2009/03/18/how-to-refactor-spring-webflow-variables-in-your-jsf-pages-with-intellij-idea/&amp;title=How+to+refactor+Spring+Webflow+variables+in+your+JSF+pages+with+IntelliJ+IDEA" rel="nofollow" title="Add to&nbsp;DZone"><img class="social_img" src="http://maxheapsize.com/wp-content/plugins/social-bookmarks/images/dzone.png" title="Add to&nbsp;DZone" alt="Add to&nbsp;DZone" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.facebook.com/sharer.php?u=http://maxheapsize.com/2009/03/18/how-to-refactor-spring-webflow-variables-in-your-jsf-pages-with-intellij-idea/" rel="nofollow" title="Add to&nbsp;Facebook"><img class="social_img" src="http://maxheapsize.com/wp-content/plugins/social-bookmarks/images/facebook.png" title="Add to&nbsp;Facebook" alt="Add to&nbsp;Facebook" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.google.com/bookmarks/mark?op=edit&amp;output=popup&amp;bkmk=http://maxheapsize.com/2009/03/18/how-to-refactor-spring-webflow-variables-in-your-jsf-pages-with-intellij-idea/&amp;title=How+to+refactor+Spring+Webflow+variables+in+your+JSF+pages+with+IntelliJ+IDEA" rel="nofollow" title="Add to&nbsp;Google Bookmarks"><img class="social_img" src="http://maxheapsize.com/wp-content/plugins/social-bookmarks/images/google.png" title="Add to&nbsp;Google Bookmarks" alt="Add to&nbsp;Google Bookmarks" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.mister-wong.com/index.php?action=addurl&amp;bm_url=http://maxheapsize.com/2009/03/18/how-to-refactor-spring-webflow-variables-in-your-jsf-pages-with-intellij-idea/&amp;bm_description=How+to+refactor+Spring+Webflow+variables+in+your+JSF+pages+with+IntelliJ+IDEA" rel="nofollow" title="Add to&nbsp;Mister Wong"><img class="social_img" src="http://maxheapsize.com/wp-content/plugins/social-bookmarks/images/misterwong.png" title="Add to&nbsp;Mister Wong" alt="Add to&nbsp;Mister Wong" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://reddit.com/submit?url=http://maxheapsize.com/2009/03/18/how-to-refactor-spring-webflow-variables-in-your-jsf-pages-with-intellij-idea/&amp;title=How+to+refactor+Spring+Webflow+variables+in+your+JSF+pages+with+IntelliJ+IDEA" rel="nofollow" title="Add to&nbsp;reddit"><img class="social_img" src="http://maxheapsize.com/wp-content/plugins/social-bookmarks/images/reddit.png" title="Add to&nbsp;reddit" alt="Add to&nbsp;reddit" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.stumbleupon.com/submit.php?url=http://maxheapsize.com/2009/03/18/how-to-refactor-spring-webflow-variables-in-your-jsf-pages-with-intellij-idea/&amp;title=How+to+refactor+Spring+Webflow+variables+in+your+JSF+pages+with+IntelliJ+IDEA" rel="nofollow" title="Add to&nbsp;Stumble Upon"><img class="social_img" src="http://maxheapsize.com/wp-content/plugins/social-bookmarks/images/stumbleupon.png" title="Add to&nbsp;Stumble Upon" alt="Add to&nbsp;Stumble Upon" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.sphere.com/sphereit/http://maxheapsize.com/2009/03/18/how-to-refactor-spring-webflow-variables-in-your-jsf-pages-with-intellij-idea/" rel="nofollow" title="Add to&nbsp;SphereIt"><img class="social_img" src="http://maxheapsize.com/wp-content/plugins/social-bookmarks/images/sphereit.png" title="Add to&nbsp;SphereIt" alt="Add to&nbsp;SphereIt" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.spurl.net/spurl.php?url=http://maxheapsize.com/2009/03/18/how-to-refactor-spring-webflow-variables-in-your-jsf-pages-with-intellij-idea/&amp;title=How+to+refactor+Spring+Webflow+variables+in+your+JSF+pages+with+IntelliJ+IDEA" rel="nofollow" title="Add to&nbsp;Spurl"><img class="social_img" src="http://maxheapsize.com/wp-content/plugins/social-bookmarks/images/spurl.png" title="Add to&nbsp;Spurl" alt="Add to&nbsp;Spurl" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.technorati.com/faves?add=http://maxheapsize.com/2009/03/18/how-to-refactor-spring-webflow-variables-in-your-jsf-pages-with-intellij-idea/" rel="nofollow" title="Add to&nbsp;Technorati"><img class="social_img" src="http://maxheapsize.com/wp-content/plugins/social-bookmarks/images/technorati.png" title="Add to&nbsp;Technorati" alt="Add to&nbsp;Technorati" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://twitter.com/home/?status=Check+out+How+to+refactor+Spring+Webflow+variables+in+your+JSF+pages+with+IntelliJ+IDEA+@+http://maxheapsize.com/2009/03/18/how-to-refactor-spring-webflow-variables-in-your-jsf-pages-with-intellij-idea/" rel="nofollow" title="Add to&nbsp;Twitter"><img class="social_img" src="http://maxheapsize.com/wp-content/plugins/social-bookmarks/images/twitter.png" title="Add to&nbsp;Twitter" alt="Add to&nbsp;Twitter" /></a>
<br />
</div>
</div>
<!-- Social Bookmarks END -->
]]></content:encoded>
			<wfw:commentRss>http://maxheapsize.com/2009/03/18/how-to-refactor-spring-webflow-variables-in-your-jsf-pages-with-intellij-idea/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Three reasons why you don&#8217;t want to use a Spring context in unit tests</title>
		<link>http://maxheapsize.com/2009/02/25/three-reasons-why-you-dont-want-to-use-a-spring-context-in-unit-tests/</link>
		<comments>http://maxheapsize.com/2009/02/25/three-reasons-why-you-dont-want-to-use-a-spring-context-in-unit-tests/#comments</comments>
		<pubDate>Wed, 25 Feb 2009 21:52:58 +0000</pubDate>
		<dc:creator>Oliver Wehrens</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[solid]]></category>
		<category><![CDATA[spring]]></category>
		<category><![CDATA[tdd]]></category>
		<category><![CDATA[test]]></category>

		<guid isPermaLink="false">http://maxheapsize.com/?p=90</guid>
		<description><![CDATA[You use Spring in your application everywhere. You love it. Everything gets injected and is configured by Spring. Great. Why not use the same technology to wire up your tests? The bottom line is: Starting the Spring Context all the time in your tests drags you down at the costs of development time. Here is [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fmaxheapsize.com%2F2009%2F02%2F25%2Fthree-reasons-why-you-dont-want-to-use-a-spring-context-in-unit-tests%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fmaxheapsize.com%2F2009%2F02%2F25%2Fthree-reasons-why-you-dont-want-to-use-a-spring-context-in-unit-tests%2F&amp;source=owehrens&amp;style=normal&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p>You use Spring in  your application everywhere. You love it. Everything gets injected and is configured by Spring. Great. Why not use the same technology to wire up your tests?</p>
<p>The bottom line is: Starting the Spring Context all the time in your tests drags you down at the costs of development time.</p>
<p>Here is why:</p>
<p><strong>1. Turnaround times are much faster</strong></p>
<p>When you discover a bug which might have not been covered by tests yet (this happens to me all the time) you are much faster rerunning your tests without the application context. In the application I&#8217;m working on it takes about 20-25 seconds to run a test with application context, whereas a pure unit test just takes 1 second. Now imagine this: changing some code and rerunning tests like 100 times saves you a lot of time. The <strong>tests run faster</strong>, you <strong>don&#8217;t get distracted</strong> because you could do something else in those 20-25 seconds, like browsing some web pages (you want to be efficient and use the &#8216;spare&#8217; time to read up the newest stories on infoq.com). But then you need to switch windows, read something, switch back, wonder what you did, rethink the problem and so on. In the end it costs you much more time than it  seems.</p>
<p><strong>2. Enables you pratice Test Driven Development</strong></p>
<p>Since you can run test faster without the Spring context, you can start doing Test Driven Development. Before any line of production code gets typed into your IDE, write the test. That the code does not compile is the first test, go fix it by writing the actual stub for the class which you want to test. Now onto the next test. It fails, you implement the feature and rerun the test. This goes on and on and on until you fully implemented the requirements. It forces you <strong>only to implements the things needed</strong> and not more.</p>
<p>Uncle Bob&#8217;s three rules of Test Driven Development</p>
<blockquote><p>
1. You are not allowed to write any production code unless it is to make a failing unit test pass.<br />
2. You are not allowed to write any more of a unit test than is sufficient to fail; and compilation failures are failures.<br />
3. You are not allowed to write any more production code than is sufficient to pass the one failing unit test.
</p></blockquote>
<p>Learn more about <a title="Uncle Bob about Test Driven Development" href="http://butunclebob.com/ArticleS.UncleBob.TheThreeRulesOfTdd">Test Driven Development</a> with Uncle Bob.</p>
<p>Without fast unit tests you just can&#8217;t do TDD.</p>
<p><strong>3. No corrupt test configuration<br />
</strong></p>
<p>Depending on the setup of the project you might have an extra Spring application context configuration for tests. You have to configure and keep it up to date. Things can go wrong here and will be detected very late.<br />
If you use the application context in tests you get the beans injected but sometimes you need to mock them, because you depend on an application server, a database or an external service which you don&#8217;t want to start up every time you run tests. You must be very careful with that. Using <strong>mocks</strong> with Spring application context can be <strong>very powerful but also very dangerous</strong>. You have to make sure that you remove the mocks after you are done with your tests (and reinject the right ones) or mark the methods which are injecting mocks with the DirtiesContext annotation so Spring can reload them.  I did debug forgotten DirtiesContext annotations one too many times. The mock object lives on in your context and 282 test later an exception occurs for no reason and you start debugging at a  wrong place.<br />
You will not discover those mock problems until you run the full suite of tests. Since your <strong>tests are slow</strong> you not very likely to run all the on a regular basis to see if everything  plays well together.  No, you run all tests <strong>right before checkin</strong> and then start to wonder why some tests somewhere <strong>fail</strong>. Depending on the size of the changes and the project it will require some substantial amount of time to find and fix the problem.</p>
<p>You might say: &#8220;But I need my application context in the tests. My Service which I want to test has so many dependencies, I just can&#8217;t mock them all!&#8221;</p>
<p>While this happened to me as well it might turn out that this service is maybe doing too much. Split the functionality  into smaller packages and test them individually (that&#8217;s the S in <a title="Solid Principles" href="http://butunclebob.com/ArticleS.UncleBob.PrinciplesOfOod">SOLID</a> principles). The danger in testing a complex service with mocks only is also that you might end up mocking everything and you don&#8217;t do any real tests anymore (happened to me, I threw hours of coding away).</p>
<p>There is one reason to run tests with a Spring application context. It&#8217;s just not in unit tests. If you want to test the integration with external services (like databases or webservices) you need to use it  of course. Just make sure you really just test the interaction between your service and the external interface and not more (but even that is debatable in some cases). That&#8217;s what I call an integration test.</p>
<p>Do you use Spring in your unit tests? What experiences did you make?</p>
<div class="google_plusone_widget"><g:plusone 
      count="true" href="http://maxheapsize.com/2009/02/25/three-reasons-why-you-dont-want-to-use-a-spring-context-in-unit-tests/" size="medium"></g:plusone></div><!-- Social Bookmarks BEGIN -->
<div class="social_bookmark">
<br />
<div class="d">
<br />
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://del.icio.us/post?url=http://maxheapsize.com/2009/02/25/three-reasons-why-you-dont-want-to-use-a-spring-context-in-unit-tests/&amp;title=Three+reasons+why+you+don%26%238217%3Bt+want+to+use+a+Spring+context+in+unit+tests" rel="nofollow" title="Add to&nbsp;Del.icio.us"><img class="social_img" src="http://maxheapsize.com/wp-content/plugins/social-bookmarks/images/delicious.png" title="Add to&nbsp;Del.icio.us" alt="Add to&nbsp;Del.icio.us" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://digg.com/submit?phase=2&amp;url=http://maxheapsize.com/2009/02/25/three-reasons-why-you-dont-want-to-use-a-spring-context-in-unit-tests/&amp;title=Three+reasons+why+you+don%26%238217%3Bt+want+to+use+a+Spring+context+in+unit+tests" rel="nofollow" title="Add to&nbsp;digg"><img class="social_img" src="http://maxheapsize.com/wp-content/plugins/social-bookmarks/images/digg.png" title="Add to&nbsp;digg" alt="Add to&nbsp;digg" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.dzone.com/links/add.html?description=Three+reasons+why+you+don%26%238217%3Bt+want+to+use+a+Spring+context+in+unit+tests&amp;url=http://maxheapsize.com/2009/02/25/three-reasons-why-you-dont-want-to-use-a-spring-context-in-unit-tests/&amp;title=Three+reasons+why+you+don%26%238217%3Bt+want+to+use+a+Spring+context+in+unit+tests" rel="nofollow" title="Add to&nbsp;DZone"><img class="social_img" src="http://maxheapsize.com/wp-content/plugins/social-bookmarks/images/dzone.png" title="Add to&nbsp;DZone" alt="Add to&nbsp;DZone" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.facebook.com/sharer.php?u=http://maxheapsize.com/2009/02/25/three-reasons-why-you-dont-want-to-use-a-spring-context-in-unit-tests/" rel="nofollow" title="Add to&nbsp;Facebook"><img class="social_img" src="http://maxheapsize.com/wp-content/plugins/social-bookmarks/images/facebook.png" title="Add to&nbsp;Facebook" alt="Add to&nbsp;Facebook" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.google.com/bookmarks/mark?op=edit&amp;output=popup&amp;bkmk=http://maxheapsize.com/2009/02/25/three-reasons-why-you-dont-want-to-use-a-spring-context-in-unit-tests/&amp;title=Three+reasons+why+you+don%26%238217%3Bt+want+to+use+a+Spring+context+in+unit+tests" rel="nofollow" title="Add to&nbsp;Google Bookmarks"><img class="social_img" src="http://maxheapsize.com/wp-content/plugins/social-bookmarks/images/google.png" title="Add to&nbsp;Google Bookmarks" alt="Add to&nbsp;Google Bookmarks" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.mister-wong.com/index.php?action=addurl&amp;bm_url=http://maxheapsize.com/2009/02/25/three-reasons-why-you-dont-want-to-use-a-spring-context-in-unit-tests/&amp;bm_description=Three+reasons+why+you+don%26%238217%3Bt+want+to+use+a+Spring+context+in+unit+tests" rel="nofollow" title="Add to&nbsp;Mister Wong"><img class="social_img" src="http://maxheapsize.com/wp-content/plugins/social-bookmarks/images/misterwong.png" title="Add to&nbsp;Mister Wong" alt="Add to&nbsp;Mister Wong" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://reddit.com/submit?url=http://maxheapsize.com/2009/02/25/three-reasons-why-you-dont-want-to-use-a-spring-context-in-unit-tests/&amp;title=Three+reasons+why+you+don%26%238217%3Bt+want+to+use+a+Spring+context+in+unit+tests" rel="nofollow" title="Add to&nbsp;reddit"><img class="social_img" src="http://maxheapsize.com/wp-content/plugins/social-bookmarks/images/reddit.png" title="Add to&nbsp;reddit" alt="Add to&nbsp;reddit" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.stumbleupon.com/submit.php?url=http://maxheapsize.com/2009/02/25/three-reasons-why-you-dont-want-to-use-a-spring-context-in-unit-tests/&amp;title=Three+reasons+why+you+don%26%238217%3Bt+want+to+use+a+Spring+context+in+unit+tests" rel="nofollow" title="Add to&nbsp;Stumble Upon"><img class="social_img" src="http://maxheapsize.com/wp-content/plugins/social-bookmarks/images/stumbleupon.png" title="Add to&nbsp;Stumble Upon" alt="Add to&nbsp;Stumble Upon" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.sphere.com/sphereit/http://maxheapsize.com/2009/02/25/three-reasons-why-you-dont-want-to-use-a-spring-context-in-unit-tests/" rel="nofollow" title="Add to&nbsp;SphereIt"><img class="social_img" src="http://maxheapsize.com/wp-content/plugins/social-bookmarks/images/sphereit.png" title="Add to&nbsp;SphereIt" alt="Add to&nbsp;SphereIt" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.spurl.net/spurl.php?url=http://maxheapsize.com/2009/02/25/three-reasons-why-you-dont-want-to-use-a-spring-context-in-unit-tests/&amp;title=Three+reasons+why+you+don%26%238217%3Bt+want+to+use+a+Spring+context+in+unit+tests" rel="nofollow" title="Add to&nbsp;Spurl"><img class="social_img" src="http://maxheapsize.com/wp-content/plugins/social-bookmarks/images/spurl.png" title="Add to&nbsp;Spurl" alt="Add to&nbsp;Spurl" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.technorati.com/faves?add=http://maxheapsize.com/2009/02/25/three-reasons-why-you-dont-want-to-use-a-spring-context-in-unit-tests/" rel="nofollow" title="Add to&nbsp;Technorati"><img class="social_img" src="http://maxheapsize.com/wp-content/plugins/social-bookmarks/images/technorati.png" title="Add to&nbsp;Technorati" alt="Add to&nbsp;Technorati" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://twitter.com/home/?status=Check+out+Three+reasons+why+you+don%26%238217%3Bt+want+to+use+a+Spring+context+in+unit+tests+@+http://maxheapsize.com/2009/02/25/three-reasons-why-you-dont-want-to-use-a-spring-context-in-unit-tests/" rel="nofollow" title="Add to&nbsp;Twitter"><img class="social_img" src="http://maxheapsize.com/wp-content/plugins/social-bookmarks/images/twitter.png" title="Add to&nbsp;Twitter" alt="Add to&nbsp;Twitter" /></a>
<br />
</div>
</div>
<!-- Social Bookmarks END -->
]]></content:encoded>
			<wfw:commentRss>http://maxheapsize.com/2009/02/25/three-reasons-why-you-dont-want-to-use-a-spring-context-in-unit-tests/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

