<?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>Trux &#187; junit</title>
	<atom:link href="http://trux.info/tag/junit/feed/" rel="self" type="application/rss+xml" />
	<link>http://trux.info</link>
	<description>Partage de mes astuces et découvertes</description>
	<lastBuildDate>Tue, 09 Jun 2009 19:47:29 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>assertThat(JUnit4, canHave(betterSyntax));</title>
		<link>http://trux.info/2009/03/assertthatjunit4-canhavebettersyntax/</link>
		<comments>http://trux.info/2009/03/assertthatjunit4-canhavebettersyntax/#comments</comments>
		<pubDate>Tue, 31 Mar 2009 20:50:54 +0000</pubDate>
		<dc:creator>chris</dc:creator>
				<category><![CDATA[Informatique]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[jmock]]></category>
		<category><![CDATA[junit]]></category>
		<category><![CDATA[test]]></category>

		<guid isPermaLink="false">http://trux.info/?p=113</guid>
		<description><![CDATA[Lors de l&#8217;écriture de tests unitaires, on peut se retrouver à écrire ce genre de code :

assertEquals&#40;42, answer&#41;;
assertFalse&#40;answer == 34&#41;;
assertTrue&#40;info.matches&#40;&#34;color&#34;&#41; &#124;&#124; info.matches&#40;&#34;colour&#34;&#41;&#41;;
assertTrue&#40;colorsList.contains&#40;&#34;red&#34;&#41;&#41;;
assertTrue&#40;s instanceof MagicString&#41;;

Mais JUnit propose depuis la version 4.4 une nouvelle syntaxe à base de Matchers. En fait, cette syntaxe n&#8217;est pas totalement nouvelle : elle est empruntée à JMock qui possède déjà les [...]]]></description>
			<content:encoded><![CDATA[<p>Lors de l&#8217;écriture de tests unitaires, on peut se retrouver à écrire ce genre de code :</p>

<div class="wp_syntax"><div class="code"><pre class="java java" style="font-family:monospace;">assertEquals<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">42</span>, answer<span style="color: #009900;">&#41;</span>;
assertFalse<span style="color: #009900;">&#40;</span>answer <span style="color: #339933;">==</span> <span style="color: #cc66cc;">34</span><span style="color: #009900;">&#41;</span>;
assertTrue<span style="color: #009900;">&#40;</span>info.<span style="color: #006633;">matches</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;color&quot;</span><span style="color: #009900;">&#41;</span> || info.<span style="color: #006633;">matches</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;colour&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>;
assertTrue<span style="color: #009900;">&#40;</span>colorsList.<span style="color: #006633;">contains</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;red&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>;
assertTrue<span style="color: #009900;">&#40;</span>s <span style="color: #000000; font-weight: bold;">instanceof</span> MagicString<span style="color: #009900;">&#41;</span>;</pre></div></div>

<p>Mais JUnit propose depuis la version 4.4 une <a href="http://junit.sourceforge.net/doc/ReleaseNotes4.4.html">nouvelle syntaxe</a> à base de <a href="http://junit.org/apidocs/org/junit/matchers/JUnitMatchers.html">Matchers</a>. En fait, cette syntaxe n&#8217;est pas totalement nouvelle : elle est empruntée à <a href="http://www.jmock.org/">JMock</a> qui possède déjà les <a href="http://www.jmock.org/matchers.html">Matchers JMock</a> pour décrire les contraintes d&#8217;un mock. L&#8217;idée vient de <a href="http://joewalnes.com/">Joe Walnes</a> dans son article <a href="http://joe.truemesh.com/blog/000511.html">Flexible JUnit assertions with assertThat()</a>.</p>
<p>Les assertions précédentes peuvent alors s&#8217;écrire de la manière suivante :</p>

<div class="wp_syntax"><div class="code"><pre class="java java" style="font-family:monospace;">assertThat<span style="color: #009900;">&#40;</span>answer, is<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">42</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>;
assertThat<span style="color: #009900;">&#40;</span>answer, is<span style="color: #009900;">&#40;</span>not<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">34</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>;
assertThat<span style="color: #009900;">&#40;</span>info, either<span style="color: #009900;">&#40;</span>containsString<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;color&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">or</span><span style="color: #009900;">&#40;</span>containsString<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;colour&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>;
assertThat<span style="color: #009900;">&#40;</span>colorsList, hasItem<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;red&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>;
assertThat<span style="color: #009900;">&#40;</span>s, <span style="color: #000000; font-weight: bold;">instanceOf</span><span style="color: #009900;">&#40;</span>MagicString.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>;</pre></div></div>

<p>Les avantages sont multiples :</p>
<ul>
<li>Ça se lit mieux : le développeur communique donc mieux son intention aux autres développeurs, ce qui est essentiel !</li>
<li>Contrairement aux méthodes assertEquals et consorts, on ne risque pas de tromper dans l&#8217;ordre des arguments : la syntaxe d&#8217;assertEquals est <code>assertEquals([expected], [actual])</code> mais beaucoup de gens se trompent et écrivent <code>assertEquals(answer, 42)</code>. Avec assertThat, aucun risque.</li>
<li>Les matchers peuvent être modifiés: par exemple : <code>not(s)</code> pour inverser la condition, <code>either(s).or(t)</code> pour combiner des conditions, <code>each(s)</code> pour appliquer une condition sur chaque élément d&#8217;une collection, <code>afterFiveSeconds(s)</code> pour attendre 5 secondes avant de tester&#8230;</li>
<li>En cas d&#8217;échec de l&#8217;assertion, les messages sont beaucoup plus lisibles. Si on reprend l&#8217;exemple précédents, avec les assertXxx classiques, les messages ressembleraient à ça :
<pre>
assertEquals(42, answer);
 => expected:<42> but was:<36>
assertFalse(answer == 34);
 => junit.framework.AssertionFailedError:
assertTrue(info.matches("color") || info.matches("colour"));
 => junit.framework.AssertionFailedError:
assertTrue(colorsList.contains("red"));
 => junit.framework.AssertionFailedError:
assertTrue(s instanceof MagicString);
 => junit.framework.AssertionFailedError:
</pre>
<p>Avec assertThat, les message ressemblent plutôt à ça :
<pre>
assertThat(answer, is(42));
 => Expected: is <42>
         got: <36>
assertThat(answer, is(not(34)));
 => Expected: is not <34>
         got: <34>
assertThat(info, either(containsString("color")).or(containsString("colour")));
 => Expected: (a string containing "colour" or a string containing "color")
         got: "rainbow"
assertThat(colorsList, hasItem("red"));
 => Expected: a collection containing "hello"
         got: <[bonjour, ia orana]>
assertThat(s, instanceOf(MagicString.class));
 => Expected: an instance of sandbox.FooTest$MagicString
         got: "polop"
</pre>
<p>Inutile de dire qu&#8217;il est beaucoup plus facile de débugguer des tests écrits avec assertThat&#8230;</li>
<li>Il est plus facile d&#8217;écrire des Matcher maison plutôt que des méthodes assertXxxx maison. En plus ces Matchers pourront être réutilisés dans des tests JMock.</li>
<li>C&#8217;est sexy <img src='http://trux.info/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </li>
</ul>
<p>Pour plus d&#8217;informations, je vous recommande la lecture de <a href="http://blog.xebia.fr/2008/04/02/simplifier-les-assertions-junit-et-ameliorer-vos-tests/">ce billet</a>. Il est en français, il est très détaillé et je suis tombé dessus en écrivant cet article.</p>
]]></content:encoded>
			<wfw:commentRss>http://trux.info/2009/03/assertthatjunit4-canhavebettersyntax/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
