<?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>Matt's Blog &#187; Testing</title>
	<atom:link href="http://blog.rueedlinger.ch/category/testing/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.rueedlinger.ch</link>
	<description>Software Engineering and Java</description>
	<lastBuildDate>Fri, 03 Feb 2012 21:59:24 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>More readable and typeable&#8230;</title>
		<link>http://blog.rueedlinger.ch/2009/02/more-readable-and-typeable/</link>
		<comments>http://blog.rueedlinger.ch/2009/02/more-readable-and-typeable/#comments</comments>
		<pubDate>Thu, 26 Feb 2009 22:27:00 +0000</pubDate>
		<dc:creator>Matthias Rüedlinger</dc:creator>
				<category><![CDATA[Frameworks]]></category>
		<category><![CDATA[Testing]]></category>
		<category><![CDATA[java]]></category>

		<guid isPermaLink="false">http://blog.rueedlinger.ch/?p=42</guid>
		<description><![CDATA[Hamcrest allows you to write readable constraints, which for example can be used with an assert statement. Hamcrest is included since JUnit 4.4 and it is the first time that third-party classes have been included in JUnit. But when you &#8230; <a href="http://blog.rueedlinger.ch/2009/02/more-readable-and-typeable/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p><a href="http://code.google.com/p/hamcrest/">Hamcrest</a> allows you to write readable constraints, which for example can be used with an assert statement. <a href="http://code.google.com/p/hamcrest/">Hamcrest</a> is included since <a href="http://junit.sourceforge.net/doc/ReleaseNotes4.4.html">JUnit 4.4</a> and it is the first time that third-party classes have been included in JUnit. But when you play around with Hamcrest and the <strong>assertThat</strong> statement you don&#8217;t want go back to old complex and unreadable assert statements.</p>
<h3>JUnit and assertThat</h3>
<p>With Hamcrest it&#8217;s very easy to write readable assert statements in a JUnit test case . In the following example we want to check if a person is older than 21 years.</p>
<pre class="code">import static org.hamcrest.Matchers.*;
import static org.hamcrest.MatcherAssert.*;

...

Person person1 = new Person();
person1.setName("Bob");
person1.setAge(22);

<strong>assertThat</strong>(person1.getAge(), greaterThan(21));</pre>
<p>As you can see the assert statement is pretty straightforward. The second parameter of an assertThat statement is a Matcher which evaluates if the first parameter is greater than the integer value from the Matcher. This syntax allows you to think in terms of subject, verb, object and not in assertEquals.</p>
<p>Hamcrest uses Matcher which are responsible to evaluate a given statement. A good overview about Hamcrest is the <a href="http://code.google.com/p/hamcrest/wiki/Tutorial">official tutorial</a>.</p>
<p>JUnit currently ships with a few matchers, defined in <strong>org.hamcrest.CoreMatchers</strong> and <strong>org.junit.matchers.JUnitMatchers</strong>. To use the other matchers download the <strong>hamcrest-all.*.jar</strong> library.</p>
<h3>hamcrest-collections</h3>
<p><a href="http://code.google.com/p/hamcrest-collections/">hamcrest-collections</a> implements features such as select, reject, map, reduce and zip which can be used on collections. This is a useful library which uses Hamcrest to operate on collections.</p>
<p>The following example show how to select from a List of integers all numbers between 5 and 9.</p>
<pre class="code">import static org.hamcrest.Matchers.*;
import static org.hamcrest.MatcherAssert.*;
import static org.hamcrestcollections.Selector.*;

...

List&lt;Integer&gt; numbers = new ArrayList&lt;Integer&gt;();
for (int i = 0; i &lt;= 10; i++) {
	numbers.add(i);
}

Iterable&lt;Integer&gt; items = select(numbers, allOf(greaterThan(5), lessThan(9)));

System.out.println(items);</pre>
<p>Which genearte the following output:</p>
<pre class="code">[6, 7, 8]</pre>
<h3>Further reading&#8230;</h3>
<p>A good starting point as already mention is the <a href="http://code.google.com/p/hamcrest/">official Hamcrest Homepage</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.rueedlinger.ch/2009/02/more-readable-and-typeable/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Java based Mail Server DevNull SMTP</title>
		<link>http://blog.rueedlinger.ch/2008/10/java-based-mail-server-devnull-smtp/</link>
		<comments>http://blog.rueedlinger.ch/2008/10/java-based-mail-server-devnull-smtp/#comments</comments>
		<pubDate>Mon, 20 Oct 2008 17:56:19 +0000</pubDate>
		<dc:creator>Matthias Rüedlinger</dc:creator>
				<category><![CDATA[Testing]]></category>
		<category><![CDATA[java]]></category>

		<guid isPermaLink="false">http://rueedlinger.ch/blog/?p=3</guid>
		<description><![CDATA[I found a simple Java based SMTP Server. DevNull SMTP is a dummy SMTP server that can be used for testing purposes. It helps you see all communication between a client and the server and is very useful if you &#8230; <a href="http://blog.rueedlinger.ch/2008/10/java-based-mail-server-devnull-smtp/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>I found a simple Java based SMTP Server. <a href="http://www.aboutmyip.com/AboutMyXApp/DevNullSmtp.jsp">DevNull SMTP</a> is a dummy SMTP server that can be used for testing purposes. It helps you see all communication between a client and the server and is very useful if you are trying to find problems with your email server or a client that you wrote.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.rueedlinger.ch/2008/10/java-based-mail-server-devnull-smtp/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

