<?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>The eflow blog &#187; junit</title>
	<atom:link href="http://blog.eflow.org/archives/tag/junit/feed" rel="self" type="application/rss+xml" />
	<link>http://blog.eflow.org</link>
	<description>Insert some amazingly witty tagline here</description>
	<lastBuildDate>Fri, 09 Dec 2011 18:59:40 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.3</generator>
		<item>
		<title>Puzzling DBUnit Error</title>
		<link>http://blog.eflow.org/archives/65</link>
		<comments>http://blog.eflow.org/archives/65#comments</comments>
		<pubDate>Sat, 09 Aug 2008 13:36:47 +0000</pubDate>
		<dc:creator>Ben</dc:creator>
				<category><![CDATA[openmrs]]></category>
		<category><![CDATA[dbunit]]></category>
		<category><![CDATA[hibernate]]></category>
		<category><![CDATA[hsqldb]]></category>
		<category><![CDATA[junit]]></category>
		<category><![CDATA[testing]]></category>

		<guid isPermaLink="false">http://blog.eflow.org/?p=65</guid>
		<description><![CDATA[When writing a dbunit test for the CohortServiceTest, I encountered this non-informative error: org.dbunit.dataset.NoPrimaryKeyException: COHORT_MEMBER In the Cohort mapping file the cohort_member table is mapped like any hibernate collection: &#60;set name="memberIds" cascade="none" lazy="true" table="cohort_member"&#62; &#60;key column="cohort_id" /&#62; &#60;element column="patient_id" type="integer" /&#62; &#60;/set&#62; It looks normal enough.  Cohort.memberIds is a list of integers defined by the [...]]]></description>
			<content:encoded><![CDATA[<p>When writing a <a href="http://dbunit.sourceforge.net/">dbunit</a> test for the <a href="http://dev.openmrs.org/browser/openmrs/trunk/test/api/org/openmrs/test/api/CohortServiceTest.java">CohortServiceTest</a>, I encountered this non-informative error:</p>
<blockquote><p>org.dbunit.dataset.NoPrimaryKeyException: COHORT_MEMBER</p></blockquote>
<p>In the <a href="http://dev.openmrs.org/browser/openmrs/trunk/metadata/api/hibernate/org/openmrs/api/db/hibernate/Cohort.hbm.xml">Cohort</a> mapping file the cohort_member table is mapped like any hibernate collection:</p>
<blockquote>
<pre>&lt;set name="memberIds" cascade="none" lazy="true" table="cohort_member"&gt;
    &lt;key column="cohort_id" /&gt;
    &lt;element column="patient_id" type="integer" /&gt;
&lt;/set&gt;</pre>
</blockquote>
<p>It looks normal enough.  <a href="http://resources.openmrs.org/doc/org/openmrs/Cohort.html#getMemberIds()">Cohort.memberIds</a> is a list of integers defined by the cohort_member table.</p>
<p>Hibernate automatically creates the schema in our test hsqldb because we have hbm2ddl set to &#8220;auto&#8221;.  The problem arose here because hibernate wasn&#8217;t creating a dual primary key on the member_ids table.  Digging through the code, I found that Hibernate only assumes the primary keys are the columns that set to &#8220;not null&#8221;.</p>
<p>The corrected mapping with both columns set to be not-null:</p>
<blockquote>
<pre>&lt;set name="memberIds" cascade="none" lazy="true" table="cohort_member"&gt;
    &lt;key column="cohort_id" not-null="true"/&gt;
    &lt;element column="patient_id" type="integer" not-null="true"/&gt;
&lt;/set&gt;</pre>
</blockquote>
<p>Now hibernate creates the table with primary keys and hsql is happy again.</p>
 <img src="http://blog.eflow.org/wp-content/plugins/feed-statistics.php?view=1&post_id=65" width="1" height="1" style="display: none;" />]]></content:encoded>
			<wfw:commentRss>http://blog.eflow.org/archives/65/feed</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Migrating from Junit3 to Junit4</title>
		<link>http://blog.eflow.org/archives/63</link>
		<comments>http://blog.eflow.org/archives/63#comments</comments>
		<pubDate>Tue, 05 Aug 2008 13:37:52 +0000</pubDate>
		<dc:creator>Ben</dc:creator>
				<category><![CDATA[openmrs]]></category>
		<category><![CDATA[junit]]></category>
		<category><![CDATA[spring]]></category>
		<category><![CDATA[testing]]></category>

		<guid isPermaLink="false">http://blog.eflow.org/?p=63</guid>
		<description><![CDATA[While migrating from JUnit 3.x to the new 4.x framework, one method I found that was missing was Spring&#8216;s getLoadCount() method on the AbstractDepenpendencyInjectionTest. The value returned allowed us to tell whether a test was running by itself or in a group a tests (like with the ant junit-report target or right-clicking on /test/src and [...]]]></description>
			<content:encoded><![CDATA[<p>While migrating from <a href="http://www.junit.org/">JUnit</a> 3.x to the new 4.x framework, one method I found that was missing was <a href="http://www.springframework.org">Spring</a>&#8216;s <a href="http://static.springframework.org/spring/docs/1.2.x/api/org/springframework/test/AbstractDependencyInjectionSpringContextTests.html#getLoadCount()">getLoadCount()</a> method on the <a href='http://junit.org'><img src="http://blog.eflow.org/wp-content/uploads/2008/08/junit-logo.jpg" alt="" title="junit logo" align="right" border="0" /></a><br />
<a href="http://static.springframework.org/spring/docs/1.2.x/api/org/springframework/test/AbstractDependencyInjectionSpringContextTests.html">AbstractDepenpendencyInjectionT</a><a href="http://static.springframework.org/spring/docs/1.2.x/api/org/springframework/test/AbstractDependencyInjectionSpringContextTests.html">est</a>. The value returned allowed us to tell whether a test was running by itself or in a group a tests (like with the ant junit-report target or right-clicking on /test/src and choosing run-as junit test).</p>
<p>Having this method was useful for two reasons:</p>
<ol>
<li>A test like <a href="http://dev.openmrs.org/browser/openmrs/trunk/test/api/org/openmrs/test/CreateInitialDataSet.java">CreateInitialDataSet</a> that is meant to be configured and run alone.  If this test sees that its being run in a group, it returns early and doesn&#8217;t actually run the test</li>
<li>The module tests needed to have the application context refreshed before running.  To do this, I made the previous test mark the context as &#8220;dirty&#8221; so that the test directly following it would have a fresh app context.</li>
</ol>
<p>There were actually two separate solutions:</p>
<ol>
<li>All tests extend <a href="http://dev.openmrs.org/browser/openmrs/trunk/test/api/org/openmrs/test/BaseContextSensitiveTest.java">BaseContextSensitiveTest</a> for convenience methods and setup.  Every junit class will invoke this class and its constructor.  By adding a static variable <code>loadCount</code> and incrementing that in the constructor, I am able to know how many tests have been run up to that point.</li>
<li>The application context doesn&#8217;t need to be marked as dirty.  The dummy method could actually be removed without replacement.</li>
</ol>
 <img src="http://blog.eflow.org/wp-content/plugins/feed-statistics.php?view=1&post_id=63" width="1" height="1" style="display: none;" />]]></content:encoded>
			<wfw:commentRss>http://blog.eflow.org/archives/63/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

