<?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>GridPulse &#187; Spring Framework</title>
	<atom:link href="http://www.gridpulse.com/category/development/spring-framework/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.gridpulse.com</link>
	<description>as stimulating as black coffee and just as hard to sleep after.</description>
	<lastBuildDate>Fri, 14 May 2010 19:19:38 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Transactional unit tests that support scopes using JUnit and Spring</title>
		<link>http://www.gridpulse.com/2009/08/21/transactional-unit-tests-that-support-scopes-using-junit-and-spring/</link>
		<comments>http://www.gridpulse.com/2009/08/21/transactional-unit-tests-that-support-scopes-using-junit-and-spring/#comments</comments>
		<pubDate>Fri, 21 Aug 2009 16:45:31 +0000</pubDate>
		<dc:creator>Bogdan</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Spring Framework]]></category>
		<category><![CDATA[Unit testing]]></category>

		<guid isPermaLink="false">http://www.gridpulse.com/?p=225</guid>
		<description><![CDATA[Most of the unit tests that you write don&#8217;t need support for transactions and scopes, but if you ever want to test your web frontend code properly you&#8217;ll hit into this one. Writing transactional unit test with JUnit, Spring and Hibernate is easy, what you want to do is: - Add a transaction management bean [...]]]></description>
			<content:encoded><![CDATA[<p>Most of the unit tests that you write don&#8217;t need support for transactions and scopes, but if you ever want to test  your web frontend code properly you&#8217;ll hit into this one. </p>
<p>Writing transactional unit test with JUnit, Spring and Hibernate is easy, what you want to do is:</p>
<p>- Add a transaction management bean in your test context:<br />
<code class="prettyprint"><br />
    &lt;bean id="transactionManager"<br />
          class="org.springframework.orm.hibernate3.HibernateTransactionManager"&gt;<br />
        &lt;property name="sessionFactory" ref="sessionFactory"/&gt;<br />
    &lt;/bean&gt;<br />
</code></p>
<p>- Make your test extend <a href="http://static.springsource.org/spring/docs/2.5.x/api/org/springframework/test/context/junit4/AbstractTransactionalJUnit4SpringContextTests.html">AbstractTransactionalJUnit4SpringContextTests</a><br />
- Annotate you test class with <a href="http://static.springsource.org/spring/docs/2.5.x/api/org/springframework/test/context/transaction/TransactionConfiguration.html">@TransactionConfiguration</a> and <a href="http://static.springsource.org/spring/docs/2.5.x/api/org/springframework/transaction/annotation/Transactional.html">@Transactional</a></p>
<p>If your transaction demarcation is correct( surrounding your dao or service ) you should be done. This is the cleanest way that I found.</p>
<p>Everything until now is fine and dandy. The problem arises when one of the injected beans is bound to a scope. It can be any scope but for this example I&#8217;ll take the simplest scope &#8211; the &#8216;session&#8217; scope.</p>
<p>In a non-transactional situation this should be pretty straight forward, just implement <a href="http://static.springsource.org/spring/docs/2.5.x/api/org/springframework/beans/factory/config/Scope.html">Scope</a> and using a <a href="http://static.springsource.org/spring/docs/2.5.x/api/org/springframework/beans/factory/config/ConfigurableBeanFactory.html">ConfigurableBeanFactory</a> register it. The fact that we are extending <a href="http://static.springsource.org/spring/docs/2.5.x/api/org/springframework/test/context/junit4/AbstractTransactionalJUnit4SpringContextTests.html"AbstractTransactionalJUnit4SpringContextTests</a> and that our context is automatically created really kills the simple path. </p>
<p>You&#8217;ll get a <a href="http://static.springsource.org/spring/docs/2.0.x/api/org/springframework/beans/factory/BeanCreationException.html">org.springframework.beans.factory.BeanCreationException</a> with a nested &#8220;java.lang.IllegalStateException: No Scope registered for scope &#8216;session&#8217;&#8221;.</p>
<p>But do not despair, there is simple solution available and it&#8217;s called <a href="http://static.springsource.org/spring/docs/2.5.6/api/org/springframework/beans/factory/config/CustomScopeConfigurer.html">CustomScopeConfigurer</a>.</p>
<p>The fastest and cleanest way to use it is:<br />
- Create a simple class that will be used as our scope manager. Let&#8217;s call it <a href="http://code.google.com/p/alexandria-umbrella/source/browse/trunk/code/xandria-tooling/xandria-translator/xandria-translator-core/src/test/java/com/gridpulse/xandria/translator/MockSessionScope.java">MockSessionScope</a>.</p>
<p><img src="http://www.gridpulse.com/wp-content/uploads/2009/08/code.jpg" alt="code" title="code" width="630" height="378" class="aligncenter size-full wp-image-238" /></p>
<p>- In your test context, create a <a href="http://static.springsource.org/spring/docs/2.5.6/api/org/springframework/beans/factory/config/CustomScopeConfigurer.html">CustomScopeConfigurer</a> that will automatically register your scope on context creation:<br />
<code class="prettyprint"><br />
    &lt;bean class="org.springframework.beans.factory.config.CustomScopeConfigurer"&gt;<br />
        &lt;property name="scopes"&gt;<br />
            &lt;map&gt;<br />
                &lt;entry key="session"&gt;<br />
                    &lt;bean class="com.gridpulse.xandria.translator.MockSessionScope"/&gt;<br />
                &lt;/entry&gt;<br />
            &lt;/map&gt;<br />
        &lt;/property&gt;<br />
    &lt;/bean&gt;<br />
</code></p>
<p><b>Run your tests and you&#8217;re done!</b></p>
]]></content:encoded>
			<wfw:commentRss>http://www.gridpulse.com/2009/08/21/transactional-unit-tests-that-support-scopes-using-junit-and-spring/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
