<?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>Schauderhaft &#187; testing</title>
	<atom:link href="http://blog.schauderhaft.de/tag/testing/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.schauderhaft.de</link>
	<description>Softwaredevelopment, Projectmanagement, Qualitymanagement and all things &#34;schauderhaft&#34;</description>
	<lastBuildDate>Sun, 25 Jul 2010 14:04:18 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>More on JUnit Theories</title>
		<link>http://blog.schauderhaft.de/2010/02/07/junit-theories/</link>
		<comments>http://blog.schauderhaft.de/2010/02/07/junit-theories/#comments</comments>
		<pubDate>Sun, 07 Feb 2010 13:02:21 +0000</pubDate>
		<dc:creator>Jens Schauder</dc:creator>
				<category><![CDATA[Softwaredevelopment]]></category>
		<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[testing]]></category>
		<category><![CDATA[theory]]></category>

		<guid isPermaLink="false">http://blog.schauderhaft.de/?p=404</guid>
		<description><![CDATA[In my last blog post, I described how to use JUnit Theories to create large amounts of test runs, with very limited amount of work, like so:

import static org.junit.Assume.assumeTrue;


@RunWith&#40;Theories.class&#41;


public class TheorieTest &#123;


&#160;


&#160;@DataPoint


&#160;public static String a = &#34;a&#34;;


&#160;


&#160;@DataPoint


&#160;public static String b = &#34;bb&#34;;


&#160;


&#160;@DataPoint


&#160;public static String c = &#34;ccc&#34;;


&#160;


&#160;@Theory


&#160;public void stringTest&#40;String x, String y&#41; &#123;


&#160; assumeTrue&#40;x.length&#40;&#41; &#38;gt; [...]]]></description>
			<content:encoded><![CDATA[<p>In my last <a href="/2010/01/31/new-feature-of-junit-theories/">blog post</a>, I described how to use JUnit Theories to create large amounts of test runs, with very limited amount of work, like so:</p>
<div class="geshi no java">
<div class="head">import static org.junit.Assume.assumeTrue;</div>
<ol>
<li class="li1">
<div class="de1">@RunWith<span class="br0">&#40;</span>Theories.<span class="kw2">class</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="kw2">public</span> <span class="kw2">class</span> TheorieTest <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;@DataPoint</div>
</li>
<li class="li1">
<div class="de1">&nbsp;<span class="kw2">public</span> <span class="kw2">static</span> <span class="kw3">String</span> a = <span class="st0">&quot;a&quot;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;@DataPoint</div>
</li>
<li class="li1">
<div class="de1">&nbsp;<span class="kw2">public</span> <span class="kw2">static</span> <span class="kw3">String</span> b = <span class="st0">&quot;bb&quot;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;@DataPoint</div>
</li>
<li class="li1">
<div class="de1">&nbsp;<span class="kw2">public</span> <span class="kw2">static</span> <span class="kw3">String</span> c = <span class="st0">&quot;ccc&quot;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;@Theory</div>
</li>
<li class="li1">
<div class="de1">&nbsp;<span class="kw2">public</span> <span class="kw4">void</span> stringTest<span class="br0">&#40;</span><span class="kw3">String</span> x, <span class="kw3">String</span> y<span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; assumeTrue<span class="br0">&#40;</span>x.<span class="me1">length</span><span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="sy0">&amp;</span>gt<span class="sy0">;</span> <span class="nu0">1</span><span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw3">System</span>.<span class="me1">out</span>.<span class="me1">println</span><span class="br0">&#40;</span>x + <span class="st0">&quot; &quot;</span> + y<span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;<span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
</ol>
</div>
<p>The trick is simple to provide data points for every parameter type of the test method. The JUnit Theories Runner will call the test method with every possible combination of datapoints. If you think a little about it you will soon realize some of the limitations of this approach:</p>
<ul>
<li>You&#8217;ll soon end up with lots of data point fields cluttering your code</li>
<li>Parameters of the same type will receive the same set of parameters, even when the usable range of inputs is completely different.</li>
</ul>
<p>Fortunately the developers of JUnit provided really nice solutions to these problems.</p>
<p>Instead of specifying single data points, you can provide a full array of datapoints using the <tt>@Datapoints</tt> annotation, like so (add imports for good measure):</p>
<div class="geshi no java">
<ol>
<li class="li1">
<div class="de1">@RunWith<span class="br0">&#40;</span>Theories.<span class="kw2">class</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="kw2">public</span> <span class="kw2">class</span> TheorieTest <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;@DataPoints</div>
</li>
<li class="li1">
<div class="de1">&nbsp;<span class="kw2">public</span> <span class="kw2">static</span> <span class="kw3">String</span><span class="br0">&#91;</span><span class="br0">&#93;</span> a = <span class="br0">&#123;</span> <span class="st0">&quot;a&quot;</span>, <span class="st0">&quot;bb&quot;</span>, <span class="st0">&quot;ccc&quot;</span> <span class="br0">&#125;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;@DataPoints</div>
</li>
<li class="li1">
<div class="de1">&nbsp;<span class="kw2">public</span> <span class="kw2">static</span> <span class="kw3">Integer</span><span class="br0">&#91;</span><span class="br0">&#93;</span> j = <span class="br0">&#123;</span> <span class="nu0">1</span>, <span class="nu0">2</span>, <span class="nu0">3</span> <span class="br0">&#125;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;@Theory</div>
</li>
<li class="li1">
<div class="de1">&nbsp;<span class="kw2">public</span> <span class="kw4">void</span> someTest<span class="br0">&#40;</span><span class="kw3">String</span> x, <span class="kw3">Integer</span> y<span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; assumeTrue<span class="br0">&#40;</span>x.<span class="me1">length</span><span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="sy0">&gt;</span> <span class="nu0">1</span><span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw3">System</span>.<span class="me1">out</span>.<span class="me1">println</span><span class="br0">&#40;</span>x + <span class="st0">&quot; &quot;</span> + y<span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;<span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
</ol>
</div>
<p>This of course is much less verbose. Instead of an array you may provide a method returning an array, or at least it looks like this should be possible. But when I tried it JUnit seemed unable to handle the types correctly resulting in IllegalArgumentExceptions. Guess I&#8217;ll have to file a bug when finished with this article &#8230; </p>
<p>But we still need to take care of parameters which have the same type, but very different meaning and therefore different useful values. The clean OO way of doing things would be to get rid of the generic types like <tt>String</tt> and use stronger types like <tt>CreditCardNumber</tt> or <tt>Name</tt> instead. But then in a perfect world we wouldn&#8217;t need tests, because our programs wouldn&#8217;t contain any bugs to begin with. So lets try this instead (Again imports omitted):</p>
<div class="geshi no java">
<ol>
<li class="li1">
<div class="de1">@Retention<span class="br0">&#40;</span>RetentionPolicy.<span class="me1">RUNTIME</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">@ParametersSuppliedBy<span class="br0">&#40;</span>CreditCardSupplier.<span class="kw2">class</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="kw2">public</span> @<span class="kw2">interface</span> AllCreditCards <span class="br0">&#123;</span><span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="co1">//&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">@Retention<span class="br0">&#40;</span>RetentionPolicy.<span class="me1">RUNTIME</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">@ParametersSuppliedBy<span class="br0">&#40;</span>NameSupplier.<span class="kw2">class</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="kw2">public</span> @<span class="kw2">interface</span> AllNames <span class="br0">&#123;</span><span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="co1">//&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="kw2">public</span> <span class="kw2">class</span> CreditCardSupplier <span class="kw2">extends</span> ParameterSupplier <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;@Override</div>
</li>
<li class="li1">
<div class="de1">&nbsp;<span class="kw2">public</span> List<span class="sy0">&lt;</span>PotentialAssignment<span class="sy0">&gt;</span> getValueSources<span class="br0">&#40;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp;ParameterSignature signature<span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; ArrayList<span class="sy0">&lt;</span>PotentialAssignment<span class="sy0">&gt;</span> result = <span class="kw2">new</span> ArrayList<span class="sy0">&lt;</span>PotentialAssignment<span class="sy0">&gt;</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; result.<span class="me1">add</span><span class="br0">&#40;</span>PotentialAssignment.<span class="me1">forValue</span><span class="br0">&#40;</span><span class="st0">&quot;Amex&quot;</span>, <span class="st0">&quot;Amex&quot;</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; result.<span class="me1">add</span><span class="br0">&#40;</span>PotentialAssignment.<span class="me1">forValue</span><span class="br0">&#40;</span><span class="st0">&quot;Master&quot;</span>, <span class="st0">&quot;Master&quot;</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; result.<span class="me1">add</span><span class="br0">&#40;</span>PotentialAssignment.<span class="me1">forValue</span><span class="br0">&#40;</span><span class="st0">&quot;Visa&quot;</span>, <span class="st0">&quot;Visa&quot;</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw2">return</span> result<span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;<span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="co1">//&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="kw2">public</span> <span class="kw2">class</span> NameSupplier <span class="kw2">extends</span> ParameterSupplier <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;@Override</div>
</li>
<li class="li1">
<div class="de1">&nbsp;<span class="kw2">public</span> List<span class="sy0">&lt;</span>PotentialAssignment<span class="sy0">&gt;</span> getValueSources<span class="br0">&#40;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp;ParameterSignature signature<span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; AllNames annotation = signature.<span class="me1">getAnnotation</span><span class="br0">&#40;</span>AllNames.<span class="kw2">class</span><span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw3">System</span>.<span class="me1">out</span>.<span class="me1">println</span><span class="br0">&#40;</span><span class="st0">&quot;just wanted to show that I can access it &quot;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; + annotation<span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; ArrayList<span class="sy0">&lt;</span>PotentialAssignment<span class="sy0">&gt;</span> result = <span class="kw2">new</span> ArrayList<span class="sy0">&lt;</span>PotentialAssignment<span class="sy0">&gt;</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; result.<span class="me1">add</span><span class="br0">&#40;</span>PotentialAssignment.<span class="me1">forValue</span><span class="br0">&#40;</span><span class="st0">&quot;Alf&quot;</span>, <span class="st0">&quot;Alf&quot;</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; result.<span class="me1">add</span><span class="br0">&#40;</span>PotentialAssignment.<span class="me1">forValue</span><span class="br0">&#40;</span><span class="st0">&quot;Willie&quot;</span>, <span class="st0">&quot;Willie&quot;</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; result.<span class="me1">add</span><span class="br0">&#40;</span>PotentialAssignment.<span class="me1">forValue</span><span class="br0">&#40;</span><span class="st0">&quot;Tanner&quot;</span>, <span class="st0">&quot;Tanner&quot;</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; result.<span class="me1">add</span><span class="br0">&#40;</span>PotentialAssignment.<span class="me1">forValue</span><span class="br0">&#40;</span><span class="st0">&quot;Cat&quot;</span>, <span class="st0">&quot;Cat&quot;</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw2">return</span> result<span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;<span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="co1">//&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">@RunWith<span class="br0">&#40;</span>Theories.<span class="kw2">class</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="kw2">public</span> <span class="kw2">class</span> SuppliedByTest <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;@Theory</div>
</li>
<li class="li1">
<div class="de1">&nbsp;<span class="kw2">public</span> <span class="kw4">void</span> imagineThisIsATest<span class="br0">&#40;</span>@AllCreditCards <span class="kw3">String</span> x, @AllNames <span class="kw3">String</span> y<span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw3">System</span>.<span class="me1">out</span>.<span class="me1">println</span><span class="br0">&#40;</span><span class="st0">&quot;consider &quot;</span> + x + <span class="st0">&quot; / &quot;</span> + y + <span class="st0">&quot; tested.&quot;</span><span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;<span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;@Theory</div>
</li>
<li class="li1">
<div class="de1">&nbsp;<span class="kw2">public</span> <span class="kw4">void</span> testIntegers<span class="br0">&#40;</span>@TestedOn<span class="br0">&#40;</span>ints = <span class="br0">&#123;</span> <span class="nu0">2</span>, <span class="nu0">3</span>, <span class="nu0">4</span>, <span class="nu0">7</span>, <span class="nu0">13</span>, <span class="nu0">23</span>, <span class="nu0">42</span> <span class="br0">&#125;</span><span class="br0">&#41;</span> <span class="kw4">int</span> i<span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw3">System</span>.<span class="me1">out</span>.<span class="me1">println</span><span class="br0">&#40;</span>i<span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;<span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
</ol>
</div>
<p>Wow, thats a lot of code. Just look at the last piece and see what appears in the console when we run it:</p>
<pre>
just wanted to show that I can access it @de.schauderhaft.junit.theories.AllNames()
consider Amex / Alf tested.
consider Amex / Willie tested.
consider Amex / Tanner tested.
consider Amex / Cat tested.
just wanted to show that I can access it @de.schauderhaft.junit.theories.AllNames()
consider Master / Alf tested.
consider Master / Willie tested.
consider Master / Tanner tested.
consider Master / Cat tested.
just wanted to show that I can access it @de.schauderhaft.junit.theories.AllNames()
consider Visa / Alf tested.
consider Visa / Willie tested.
consider Visa / Tanner tested.
consider Visa / Cat tested.
2
3
4
7
13
23
42
</pre>
<p>Have a look at the row beginning with: &#8220;consider&#8221;. Obviously the Theory <tt>imagineThisIsATest</tt> gets fed with the values from the <tt>CreditCardSupplier</tt> and <tt>NameSupplier</tt>. The parameters and the &#8216;Suppliers&#8217; are connected by the two annotations <tt>@AllNames</tt> and <tt>AllCreditCards</tt>. So whenever you have a parameter to a theory where the type alone is not sufficient for identifying the kind of values that should get used, you can simple create an annotation, which itself is annotated with a reference to a <tt>ParameterSupplier</tt> class and you are all set. You might think this is a lot of code for supplying a handful of parameters. You are right, but remember, that you can reuse your suppliers wherever you need names or credit card values in your tests. </p>
<p>Now let&#8217;s look at the first line of the output:<br />
<tt>just wanted to show that I can access it @de.schauderhaft.junit.theories.AllNames()</tt><br />
It simply shows of that you get access to the annotation (and actually the signature of the compete test method. This can be very useful, when you want your supplier to behave differently for different theories. Have a look at the <tt>NameSupplier</tt> above to see how this works.</p>
<p>JUnit actually comes with an example where this is used, and I demonstrated it with the other theory in the demonstration code above. The <tt>@TestedOn</tt> annotation takes an array of values to be used as data points for the annotated parameter.</p>
<p>Thats it for today. I hope the power of theories became obvious, as well as the power you have as a developer to extend that mechanism. Again be warned: All this nice stuff is in a package named experimental for good reason. If you use it, you might find bugs, and thing will likely change at least in name in an upcoming version. Taking about versions, I am using junit4.8.1 for the examples.</p>
<p>For next week the conclusion of the little series about JUnit theories is planned, with a few thoughts on use and danger of this kind of testing.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.schauderhaft.de/2010/02/07/junit-theories/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>New Feature of JUnit: Theories</title>
		<link>http://blog.schauderhaft.de/2010/01/31/new-feature-of-junit-theories/</link>
		<comments>http://blog.schauderhaft.de/2010/01/31/new-feature-of-junit-theories/#comments</comments>
		<pubDate>Sun, 31 Jan 2010 20:14:25 +0000</pubDate>
		<dc:creator>Jens Schauder</dc:creator>
				<category><![CDATA[Softwaredevelopment]]></category>
		<category><![CDATA[junit]]></category>
		<category><![CDATA[testing]]></category>
		<category><![CDATA[theory]]></category>

		<guid isPermaLink="false">http://blog.schauderhaft.de/?p=399</guid>
		<description><![CDATA[A couple of months ago I blogged about JUnit Rules, one of the new features in JUnit. While fooling around with JUnit Rules, I found a couple more features that you might be interested in. So here it comes: Theories! It turns out Theories are really a piece of cake. Try this:

import static org.junit.Assume.assumeTrue;


import org.junit.experimental.theories.DataPoint;


import [...]]]></description>
			<content:encoded><![CDATA[<p>A couple of months ago I blogged about <a href="/2009/10/04/junit-rules/">JUnit Rules</a>, one of the new features in JUnit. While fooling around with JUnit Rules, I found a couple more features that you might be interested in. So here it comes: Theories! It turns out Theories are really a piece of cake. Try this:</p>
<div class="geshi no java">
<div class="head">import static org.junit.Assume.assumeTrue;</div>
<ol>
<li class="li1">
<div class="de1"><span class="co2">import org.junit.experimental.theories.DataPoint;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="co2">import org.junit.experimental.theories.Theories;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="co2">import org.junit.experimental.theories.Theory;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="co2">import org.junit.runner.RunWith;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">@RunWith<span class="br0">&#40;</span>Theories.<span class="kw2">class</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="kw2">public</span> <span class="kw2">class</span> TheorieTest <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;@DataPoint</div>
</li>
<li class="li1">
<div class="de1">&nbsp;<span class="kw2">public</span> <span class="kw2">static</span> <span class="kw3">String</span> a = <span class="st0">&quot;a&quot;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;@DataPoint</div>
</li>
<li class="li1">
<div class="de1">&nbsp;<span class="kw2">public</span> <span class="kw2">static</span> <span class="kw3">String</span> b = <span class="st0">&quot;bb&quot;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;@DataPoint</div>
</li>
<li class="li1">
<div class="de1">&nbsp;<span class="kw2">public</span> <span class="kw2">static</span> <span class="kw3">String</span> c = <span class="st0">&quot;ccc&quot;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;@Theory</div>
</li>
<li class="li1">
<div class="de1">&nbsp;<span class="kw2">public</span> <span class="kw4">void</span> stringTest<span class="br0">&#40;</span><span class="kw3">String</span> x, <span class="kw3">String</span> y<span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; assumeTrue<span class="br0">&#40;</span>x.<span class="me1">length</span><span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="sy0">&amp;</span>gt<span class="sy0">;</span> <span class="nu0">1</span><span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw3">System</span>.<span class="me1">out</span>.<span class="me1">println</span><span class="br0">&#40;</span>x + <span class="st0">&quot; &quot;</span> + y<span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;<span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
</ol>
</div>
<p>When you run this you&#8217;ll get this as an output (and one successfull test):</p>
<pre>bb a
bb bb
bb ccc
ccc a
ccc bb
ccc ccc</pre>
<p>So what is going on? The first thing to note is that this test is executed by a specialized runner of type Theories. This runner executes all the public methods annotated with @Theory. Differently from normal tests theories have parameters. In order to fill these parameters with values, the Theories runner uses all the public fields of matching Type and annotated with @DataPoint. When you take a look at the output, it should be obvious why this is more powerful then <a href="http://ourcraft.wordpress.com/2008/08/27/writing-a-parameterized-junit-test/">parameterized Tests</a>: Every combination of values is tried, thus with e.g. 4 parameters with 4 distinct values each, you end up with 256 test runs.</p>
<p>The idea is to specify a theory about the object under test, that holds for a large class of states and parameters.  Then you provide those as parameters to the test method which will test the theory.</p>
<p>A probably very common case is that a theory is known not to be valid for certain cases. You can exclude these from a test using the Assume class. If an assumption doesn&#8217;t hold, the test is silently ignored. This is used in the example above to prevent &#8216;a&#8217; to be used as a first parameter.</p>
<p>There are some more tweaks to Theories in JUnit, which I will cover in a later blog. Until then enjoy the new feature. But keep in mind the package in which theories reside up to now: <tt>org.junit.<strong>experimental</strong>.theories </tt> so I&#8217;d expect some changes in the API and at least a change in package name.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.schauderhaft.de/2010/01/31/new-feature-of-junit-theories/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>New Project&#8217;s Resolution</title>
		<link>http://blog.schauderhaft.de/2010/01/10/new-projects-resolution/</link>
		<comments>http://blog.schauderhaft.de/2010/01/10/new-projects-resolution/#comments</comments>
		<pubDate>Sun, 10 Jan 2010 10:41:07 +0000</pubDate>
		<dc:creator>Jens Schauder</dc:creator>
				<category><![CDATA[Softwaredevelopment]]></category>
		<category><![CDATA[clean code]]></category>
		<category><![CDATA[dependency injection]]></category>
		<category><![CDATA[resolution]]></category>
		<category><![CDATA[testing]]></category>
		<category><![CDATA[toxicity]]></category>

		<guid isPermaLink="false">http://blog.schauderhaft.de/?p=372</guid>
		<description><![CDATA[In a couple of days I&#8217;ll start working on a new project. Actually it is an project that I worked one or two years ago. I think I did decent job last time. But there is always lots of room for improvement. So today I want to list a couple of things I want to [...]]]></description>
			<content:encoded><![CDATA[<div id="attachment_374" class="wp-caption alignleft" style="width: 310px"><a href="http://www.sxc.hu/photo/1216962"><img class="size-medium wp-image-374" title="Fireworks" src="http://blog.schauderhaft.de/wp-content/uploads/2010/01/1216962_13713831-300x200.jpg" alt="" width="300" height="200" /></a><p class="wp-caption-text">Fireworks</p></div>
<p>In a couple of days I&#8217;ll start working on a new project. Actually it is an project that I worked one or two years ago. I think I did decent job last time. But there is always lots of room for improvement. So today I want to list a couple of things I want to do bettern than the last time.</p>
<p><strong>More Tests</strong> &#8211; When working on that project I was the only person with real experience in automated testing. I am proud that we had a test suite and that this test suite still exists and put to good use and actually grew during the last months. Yet I consider the coverage of the tests not sufficient by far. So for every code I write, I will add an extensive set of test. I&#8217;ll also add tests for existing code, when refactoring.</p>
<p><strong>Earlier Tests</strong> &#8211; One reason we didn&#8217;t produce as many tests as I would wish today is that we didn&#8217;t do TDD. It probably was a correct decission the last time, since it would have been just to much. But this time I will practice TDD as good as I can.</p>
<p><strong>Cleaner Code</strong> &#8211; One of the first tests I wrote back then was a test to ensure that we don&#8217;t have circular dependencies. We succeeded in this, but still parts of the system a pretty tangled. We gonna improve that. Appart from the knowledge I took from <a href="http://www.amazon.com/gp/product/0132350882?ie=UTF8&amp;tag=schauderhaft-20&amp;linkCode=as2&amp;camp=1789&amp;creative=390957&amp;creativeASIN=0132350882">Clean Code</a> I&#8217;ll plan to use the <a href="http://erik.doernenburg.com/2008/11/how-toxic-is-your-code/">toxicity metric</a> as an important tool for that.</p>
<p><strong>Dependency Injection</strong> &#8211; We considered using Spring and decided against it, because we already had about 20 libraries we hardly knew in the project. Again at that time it was a reasonable decision. But in the meantime I learned what we could have gained by using Spring. I&#8217;ll propose using it, but even when the team declines again, I&#8217;ll be able to use the concept in order to improve the code base.</p>
<p>So these are the things I want to improve on in the next project. What are the things you want to improve on in your next project?</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.schauderhaft.de/2010/01/10/new-projects-resolution/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>New Feature of JUnit: Rules</title>
		<link>http://blog.schauderhaft.de/2009/10/04/junit-rules/</link>
		<comments>http://blog.schauderhaft.de/2009/10/04/junit-rules/#comments</comments>
		<pubDate>Sun, 04 Oct 2009 10:12:20 +0000</pubDate>
		<dc:creator>Jens Schauder</dc:creator>
				<category><![CDATA[Softwaredevelopment]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[junit]]></category>
		<category><![CDATA[Rule]]></category>
		<category><![CDATA[testing]]></category>

		<guid isPermaLink="false">http://blog.schauderhaft.de/?p=252</guid>
		<description><![CDATA[I am always surprised how many unknown feature hide in a supposedly simple library. Todays example is JUnit. When inspecting the newest version (4.7) I noted an annotation I hadn&#8217;t noticed before: @Rule. WTF? I am looking at a testing framework and not at a rules engine, am I? So naturally I tried to find [...]]]></description>
			<content:encoded><![CDATA[<p><div id="attachment_258" class="wp-caption alignleft" style="width: 310px"><img class="size-medium wp-image-258" title="1117482_76414816" src="http://blog.schauderhaft.de/wp-content/uploads/2009/10/1117482_76414816-300x200.jpg" alt="card board box" width="300" height="200" /><p class="wp-caption-text">card board box</p></div><br />
I am always surprised how many unknown feature hide in a supposedly simple library. Todays example is <a href="http://junit.org/">JUnit</a>. When inspecting the newest version (4.7) I noted an annotation I hadn&#8217;t noticed before: <tt>@Rule</tt>. WTF? I am looking at a testing framework and not at a rules engine, am I? So naturally I tried to find out what it is good for, and since not to much documentation is available my little research resulted in this blog post. Enjoy.</p>
<p>The purpose of the <tt>@Rule</tt> annotation is to mark public fields of a test class. These fields  must be of type <tt>MethodRule</tt>, or an implementing class. Such <tt>MethodRule</tt>s behave similar to a AOP aspects, of course without use of any AOP library and specialized for Tests. They can execute code before, after or instead of a test method.  Example use cases listed in the <a href="http://github.com/KentBeck/junit/raw/23ffc6baf5768057e366e183e53f4dfa86fbb005/doc/ReleaseNotes4.7.txt">release notes</a> include:</p>
<ul>
<li>Notification on tests</li>
<li>Setting up or tearing down resources, especially when they are used in multiple test classes</li>
<li>Special checks performed after every test, possibly causing a test to fail.</li>
<li>Making information about the test available inside the test</li>
</ul>
<p>But ready made examples are boring, so I decided to try to implement something I was looking for all the time.</p>
<p>When writing acceptance tests with <a href="http://fit.c2.com/">Fit</a> or a similiar framework, you might end up with tons of failing tests, simply because the features aren&#8217;t implemented yet. But I don&#8217;t want this red lights hide a real red light, i.e. a test that worked before, but fails now. Therefore I&#8217;d like tests to get ignored just as if they where accordingly annotated, until they succeed for the first time. After that they should run (and possibly fail) just as a normal.</p>
<p>Let&#8217;s get started. First we need a little test case that can fail or succeed as we want. And this test case of course needs our annotated field:</p>
<div class="geshi no java">
<div class="head">public class DummyTest {</div>
<ol>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; @Rule</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw2">public</span> IgnoreLeadingFailure ilf = <span class="kw2">new</span> IgnoreLeadingFailure<span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; @Test</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw2">public</span> <span class="kw4">void</span> testTest<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; assertTrue<span class="br0">&#40;</span><span class="kw2">false</span><span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
</ol>
</div>
<p>Please note that I stripped all package and import statements for brevity. As you can see I already named my <tt>MethodRule</tt> implementation <tt>IgnoreLeadingFailure</tt>. It needs some way to track which tests ran successfully at least once. Since I am lazy, I stored this information in a property file.</p>
<div class="geshi no java">
<div class="head">public class IgnoreLeadingFailure implements MethodRule {</div>
<ol>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw2">private</span> <span class="kw2">final</span> <span class="kw2">static</span> <span class="kw3">String</span> PROPERTY_FILE_NAME = <span class="st0">&quot;activatedTests.properties&quot;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw2">private</span> <span class="kw2">final</span> <span class="kw2">static</span> <span class="kw3">Properties</span> activatedTests = <span class="kw2">new</span> <span class="kw3">Properties</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw2">static</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">try</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; activatedTests.<span class="me1">load</span><span class="br0">&#40;</span><span class="kw2">new</span> <span class="kw3">FileInputStream</span><span class="br0">&#40;</span>PROPERTY_FILE_NAME<span class="br0">&#41;</span><span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span> <span class="kw2">catch</span> <span class="br0">&#40;</span><span class="kw3">IOException</span> e<span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">// actually this is to be expected on the first run</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw3">System</span>.<span class="me1">out</span>.<span class="me1">println</span><span class="br0">&#40;</span><span class="st0">&quot;Couldn&#39;t load Properties from file&quot;</span> + e<span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw2">public</span> <span class="kw3">Statement</span> apply<span class="br0">&#40;</span><span class="kw2">final</span> <span class="kw3">Statement</span> base, <span class="kw2">final</span> FrameworkMethod method,</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">final</span> <span class="kw3">Object</span> target<span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span>activatedTests.<span class="me1">containsKey</span><span class="br0">&#40;</span>getFullTestMethodName<span class="br0">&#40;</span>method, target<span class="br0">&#41;</span><span class="br0">&#41;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">return</span> base<span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span> <span class="kw1">else</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">return</span> <span class="kw2">new</span> <span class="kw3">Statement</span><span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">public</span> <span class="kw4">void</span> evaluate<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="kw2">throws</span> <span class="kw3">Throwable</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">try</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; base.<span class="me1">evaluate</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; activateTest<span class="br0">&#40;</span>getFullTestMethodName<span class="br0">&#40;</span>method, target<span class="br0">&#41;</span><span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span> <span class="kw2">catch</span> <span class="br0">&#40;</span><span class="kw3">Throwable</span> t<span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">throw</span> <span class="kw2">new</span> AssumptionViolatedException<span class="br0">&#40;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="st0">&quot;This test never succeeded before, and failed again with: &quot;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; + t.<span class="me1">toString</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">private</span> <span class="kw4">void</span> activateTest<span class="br0">&#40;</span><span class="kw3">String</span> fullTestMethodName<span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; activatedTests.<span class="me1">put</span><span class="br0">&#40;</span>fullTestMethodName,</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">new</span> <span class="kw3">SimpleDateFormat</span><span class="br0">&#40;</span><span class="br0">&#41;</span>.<span class="me1">format</span><span class="br0">&#40;</span><span class="kw2">new</span> <span class="kw3">Date</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">try</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; activatedTests.<span class="me1">store</span><span class="br0">&#40;</span><span class="kw2">new</span> <span class="kw3">FileOutputStream</span><span class="br0">&#40;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; PROPERTY_FILE_NAME<span class="br0">&#41;</span>,</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="st0">&quot;tests that ran successfully at least once&quot;</span><span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span> <span class="kw2">catch</span> <span class="br0">&#40;</span><span class="kw3">IOException</span> io<span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw3">System</span>.<span class="me1">out</span>.<span class="me1">println</span><span class="br0">&#40;</span><span class="st0">&quot;failed to store properties&quot;</span> + io<span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw2">private</span> <span class="kw3">String</span> getFullTestMethodName<span class="br0">&#40;</span><span class="kw2">final</span> FrameworkMethod method,</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw3">Object</span> target<span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">return</span> target.<span class="me1">getClass</span><span class="br0">&#40;</span><span class="br0">&#41;</span>.<span class="me1">getName</span><span class="br0">&#40;</span><span class="br0">&#41;</span> + <span class="st0">&quot; &quot;</span> + method.<span class="me1">getName</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
</ol>
</div>
<p>The intersting part is the single method contained in the <tt>MethodRule</tt> interface:</p>
<pre lang="java">public Statement apply(final Statement base, final FrameworkMethod method, final Object target)</pre>
<p><strong>base</strong> is the object that encapsulates the execution of the test method. The purpose of apply(..) is to provide a Statement. This Statement will get executed (or in the lingo of the interface &#8216;evaluated&#8217;). You can return just <tt><base</tt>, when you don't want to do anything, or more typically you'll return a <tt>Statement</tt> implementation, that wraps the <tt>Statement</tt> instance passed as a parameter.</p>
<p><strong>method</strong> is the test method that will eventually get called and</p>
<p><strong>target</strong> is the object containing that method. Note that you normally do not execute the method, but use it mainly to get more information about the test case the <tt>MethodRule</tt> is applied to.</p>
<p>With this information it should be easy to understand what my little class does: It checks if the test at hand was already executed successfully before. If this is the case, the same <tt>Statement</tt> is returned, that was passed as a parameter. Otherwise an anonymous implementation is returned which replaces any failure by a <tt>AssumptionViolatedException</tt>, and saves a successful test run in the property file. Note that the <tt>AssumptionViolatedException</tt> actually does not result in a ignored test, but gets displayed as a successful test, at least with the default runner, which doesn't really fit my needs, but one could easily fix that by a minor change in the runner but not without that change.</p>
<p>So here is my opinion about this new JUnit feature: It certainly can come in handy for (integration) tests, where one tends to need a lot of resources that need quite some setup and/or tear down. Implementation of a <tt>MethodRule</tt> is fairly easy, although not exactly straight forward. The biggest problem is that it is next to impossible for an average java developer who doesn't know this feature to understand what is going on. The annotated field is easily missed, since in many cases it isn't used at all in the test class. Once more all this could be straight forward when java would support natively things like aspect oriented programming or open classes.</p>
<p>If you want more information I suggest you drop by <a href="http://www.threeriversinstitute.org/blog/?p=155">this blog post about 'Interceptors' which was the name for rules</a> at the time of writing of that post.</p>
<p>So, what do you think of this feature? Any ideas for features that could be implemented using rules?</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.schauderhaft.de/2009/10/04/junit-rules/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Database Version Management &#8230; Again</title>
		<link>http://blog.schauderhaft.de/2009/02/26/database-testing-again/</link>
		<comments>http://blog.schauderhaft.de/2009/02/26/database-testing-again/#comments</comments>
		<pubDate>Thu, 26 Feb 2009 22:38:18 +0000</pubDate>
		<dc:creator>Jens Schauder</dc:creator>
				<category><![CDATA[Softwaredevelopment]]></category>
		<category><![CDATA[agile]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[software development]]></category>
		<category><![CDATA[testing]]></category>

		<guid isPermaLink="false">http://blog.schauderhaft.de/2009/02/26/database-testing-again/</guid>
		<description><![CDATA[ Pramod Sadalage author of Refactoring Databases: Evolutionary Database Design forwarded an email to the agiledatabases group announcing a new database migration tool (note: the tool is NOT writen by Pramod): dbmaintain
While I am glad that there are others worrying about the lack of basic software development tools in the database realm, I still think we [...]]]></description>
			<content:encoded><![CDATA[<p> <a href="http://databaserefactoring.com/PramodSadalage.html">Pramod Sadalage</a> author of <a href="http://www.amazon.de/gp/product/0321293533?ie=UTF8&amp;tag=schauderhafte-21&amp;linkCode=as2&amp;camp=1638&amp;creative=19454&amp;creativeASIN=0321293533">Refactoring Databases: Evolutionary Database Design</a><img src="http://www.assoc-amazon.de/e/ir?t=schauderhafte-21&amp;l=as2&amp;o=3&amp;a=0321293533" style="border: medium none  ! important; margin: 0px ! important" width="1" border="0" height="1" /> forwarded an email to the <a href="http://groups.yahoo.com/group/agileDatabases/">agiledatabases </a>group announcing a new database migration tool (note: the tool is NOT writen by Pramod): <a href="http://www.dbmaintain.org">dbmaintain</a></p>
<p>While I am glad that there are others worrying about the lack of basic software development tools in the database realm, I still think we are really missing to things.</p>
<p>First: all these tools are using small migrations, and manage those. I think this is like directly writing patch files and then using some tools to assemble class files from those. Why not maintain one file for creating the schema or the changes from one release to the next. It is much easier to maintain and the version control system will split it into changes if you need it.</p>
<p>Secondly: We now have <a href="http://code.google.com/p/dbmigrate/">dbmigrate</a>, <a href="http://migratedb.sourceforge.net/">migratedb</a>, <a href="http://www.liquibase.org/">liquibase</a>, <a href="http://www.dbmaintain.org">dbmaintain</a>, <a href="http://dbdeploy.com/">dbdeploy</a> but none seems to get real traction. Liquibase seems to be most active but even that doesn&#8217;t cause enough search traffic to show up on google trends and it is so horribly ridden with XML that I don&#8217;t even want to look at it.</p>
<p>We need a JUnit equivalent for database release management. We need somebody with some traction in the community to pick one of those tools and push it forward real hard. Pramod? Martin? Anybody? Please!<a href="http://dbdeploy.com/"><br />
</a></p>
<pre><a href="http://groups.yahoo.com/group/agileDatabases/" class="moz-txt-link-freetext">

</a></pre>
]]></content:encoded>
			<wfw:commentRss>http://blog.schauderhaft.de/2009/02/26/database-testing-again/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
