<?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>Chris Aniszczyk&#039;s (zx) diatribe &#187; java</title>
	<atom:link href="http://aniszczyk.org/tag/java/feed/" rel="self" type="application/rss+xml" />
	<link>http://aniszczyk.org</link>
	<description>work. life. open source. diatribes.</description>
	<lastBuildDate>Tue, 17 Apr 2012 17:09:40 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<item>
		<title>JUnit4 @Rule&#8217;s Rule</title>
		<link>http://aniszczyk.org/2010/06/25/junit4-rules-rule/</link>
		<comments>http://aniszczyk.org/2010/06/25/junit4-rules-rule/#comments</comments>
		<pubDate>Fri, 25 Jun 2010 14:29:29 +0000</pubDate>
		<dc:creator>Chris Aniszczyk</dc:creator>
				<category><![CDATA[work]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[testing]]></category>

		<guid isPermaLink="false">http://aniszczyk.org/?p=2561</guid>
		<description><![CDATA[Maybe I&#8217;ve been under a rock as of late, but I&#8217;ve been writing some tests as of late and came across a nifty little feature in JUnit4 called Rules while needing to verify a certain exception was being thrown. For example, let&#8217;s say we needed to verify that something threw an IAE&#8230; public class MyTest [...]]]></description>
			<content:encoded><![CDATA[<p>Maybe I&#8217;ve been under a rock as of late, but I&#8217;ve been writing some tests as of late and came across a nifty little feature in JUnit4 called <a href="http://www.infoq.com/news/2009/07/junit-4.7-rules">Rules</a> while needing to verify a certain exception was being thrown. For example, let&#8217;s say we needed to verify that something threw an IAE&#8230;</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> MyTest <span style="color: #009900;">&#123;</span>
  @Rule
  <span style="color: #000000; font-weight: bold;">public</span> ExpectedException exception <span style="color: #339933;">=</span> ExpectedException.<span style="color: #006633;">none</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  @Test
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> willThrowIAE<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    exception.<span style="color: #006633;">expect</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">IllegalArgumentException</span>.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    exception.<span style="color: #006633;">expectMessage</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;omg bad arguments&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #666666; font-style: italic;">// do crap that will throw an IAE</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>This is just the tip of the iceberg with what you can do with rules. There&#8217;s also a rule (<code>TemporaryFolder</code>) for creating temporary folders that are guaranteed to be deleted after the test is run. On top of that, it&#8217;s pretty easy to create your own rules if you need to modify test behavior to suit your needs. </p>
<p>In the end, it&#8217;s always fun to discover something new and useful in a tool you&#8217;ve been using for awhile.</p>
]]></content:encoded>
			<wfw:commentRss>http://aniszczyk.org/2010/06/25/junit4-rules-rule/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

