<?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>Giordano Scalzo&#039;s Personal Blog &#187; jdave</title>
	<atom:link href="http://giordano.scalzo.biz/tag/jdave/feed/" rel="self" type="application/rss+xml" />
	<link>http://giordano.scalzo.biz</link>
	<description>Just another useless weblog</description>
	<lastBuildDate>Fri, 09 Jul 2010 09:41:02 +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>Using JDave: A quick introduction to specs framework</title>
		<link>http://giordano.scalzo.biz/2009/09/17/using-jdave-a-quick-introduction-to-specs-framework/</link>
		<comments>http://giordano.scalzo.biz/2009/09/17/using-jdave-a-quick-introduction-to-specs-framework/#comments</comments>
		<pubDate>Thu, 17 Sep 2009 15:13:47 +0000</pubDate>
		<dc:creator>giordano scalzo</dc:creator>
				<category><![CDATA[bdd]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[tutorial]]></category>
		<category><![CDATA[eclipse]]></category>
		<category><![CDATA[jdave]]></category>

		<guid isPermaLink="false">http://giordano.scalzo.biz/?p=90</guid>
		<description><![CDATA[As second Bdd engine to try, I choose JDave, a specification oriented engine.
JBehave is, instead, user-stories-oriented: the difference is very subtle and I&#8217;m not sure I caught it completely  .
Anyway, JDaveis inspired by RSpec, at the moment the most used bdd engine, so I thought it deserved a try.
In order to compare JDave with [...]]]></description>
			<content:encoded><![CDATA[<p>As second Bdd engine to try, I choose <a href="http://www.jdave.org/">JDave</a>, a specification oriented engine.<br />
<a href="http://jbehave.org/">JBehave </a>is, instead, user-stories-oriented: the difference is very subtle and I&#8217;m not sure I caught it completely <img src='http://giordano.scalzo.biz/wp-includes/images/smilies/icon_wink.gif' alt=';-)' class='wp-smiley' /> .<br />
Anyway, <a href="http://www.jdave.org/">JDave</a>is inspired by <a href="http://rspec.info/">RSpec</a>, at the moment the most used bdd engine, so I thought it deserved a try.<br />
In order to compare <a href="http://www.jdave.org/">JDave</a> with <a href="http://jbehave.org/">JBehave</a>, I implemented the StringTemplater kata, as in my <a href="http://giordano.scalzo.biz/2009/09/04/bdd-with-jbehave/">previous post</a>.</p>
<h3>Installing JDave</h3>
<p>After creating a java project, I simply downloaded the <a href="http://www.jdave.org/resources.html">last version of JDave</a> and extracted all jar in lib directory of my project:</p>
<p><img src="http://giordano.scalzo.biz/wp-content/uploads/2009/09/bdd.png" alt="JDave Jars" title="JDave Jars" width="259" height="441" class="aligncenter size-full wp-image-97" /></p>
<p>They are a lot of jars, and I&#8217;m sure I won&#8217;t use all of them, but it&#8217;s just a try so it doesn&#8217;t deserve the time to filter only the used ones.</p>
<p><a href="http://jbehave.org/">JBehave</a> is a story runner, so each scenario must be written as:</p>
<pre><strong>Given</strong> something
<strong>When </strong>something happens
<strong>Then </strong>this happens</pre>
<p>Instead, <a href="http://www.jdave.org/">JDave</a> is a specification engine and each scenario show a behavior of a class:</p>
<pre>AThingIWantToWrite
  - ShouldDoThis
  - ShouldDoThat
  - ShouldntDoThat</pre>
<p>In other words, <a href="http://jbehave.org/">JBehave</a> is similar to <a href="http://cukes.info/">Cucumber</a>, <a href="http://www.jdave.org/">JDave</a> is similar to <a href="http://rspec.info/">RSpec</a>.</p>
<p>Writing a specification is really straightforward:<br />
first of all, we create a <code>Specification </code>object, passing the object we want to write; then we create a serie of inner classes:</p>
<pre class="brush: java">@RunWith(JDaveRunner.class)
public class StringTemplaterSpec extends Specification&lt;ThingIWantToWrite&gt; {
	public class AThingIWantToWrite {
		public void ShouldDoThis() {
		}
		public void ShouldDoThat() {
		}
        }
   ...
}</pre>
<p>As <a href="http://jbehave.org/">JBehave</a>, <a href="http://www.jdave.org/">JDave</a> is a wrapper built over JUnit, so we can use our Ide integration to run the specifications.<br />
That&#8217;s it!</p>
<h3>The code</h3>
<p>As in <a href="http://giordano.scalzo.biz/2009/09/04/bdd-with-jbehave/">JBehave try</a>, I implemented the same scenarios as in Corey Haines Video:</p>
<pre class="brush: java">package biz.scalzo.kata.stringtemplater.jdave;

import org.junit.runner.RunWith;

import jdave.Specification;
import jdave.junit4.JDaveRunner;

@RunWith(JDaveRunner.class)
public class StringTemplaterSpec extends Specification&lt;StringTemplater&gt; {
	public class AStringTemplater {
		private StringTemplater stringTemplater;

		public void create() {
			stringTemplater = new StringTemplater();
		}

		public void shouldReturnEmptyWhenAnEmptyStringIsPassed() {
			specify(stringTemplater.replace(""), must.equal(""));
		}

		public void shouldReturnTheOriginalStringWhenNoMarkersArePassed() {
			specify(stringTemplater.replace("original string"), must.equal("original string"));
		}

		public void shouldReplaceAToken() {
			specify(stringTemplater.replace("Hello, $name","name: giordano"),
					must.equal("Hello, giordano"));
		}

		public void shouldReplaceTwoTokens() {
			specify(stringTemplater.replace("Hello, $name, how a $attitude day","name: giordano, attitude:  wonderful"),
					must.equal("Hello, giordano, how a wonderful day"));
		}

		public void shouldRemoveNotProvidedMarkers() {
			specify(stringTemplater.replace("Hello, $name, how a $attitude day","name: giordano"),
					must.equal("Hello, giordano, how a  day"));
		}
	}

}</pre>
<p>The Junit view of Eclipse is very explicative of the behaviors of StringTemplater:<br />
<img src="http://giordano.scalzo.biz/wp-content/uploads/2009/09/junit_view.png" alt="JUnit View" title="JUnit View" width="491" height="326" class="aligncenter size-full wp-image-98" /></p>
<p>For sake of completeness, this is the final implementation of StringTemplater:</p>
<pre class="brush: java">package biz.scalzo.kata.stringtemplater.jdave;

import java.util.Arrays;
import java.util.Iterator;

public class StringTemplater {

	public String replace(String stringToReplace) {
		return replace(stringToReplace, "");
	}

	public String replace(String stringToReplace, String markers) {
		return replace(stringToReplace, initialIterator(markers));
	}

	private String[] tupla(String pair) {
		return pair.split(":");
	}

	private Iterator&lt;String&gt; initialIterator(String markers) {
		return Arrays.asList(markers.split(",")).iterator();
	}

	private String replace(String stringToReplace, Iterator&lt;String&gt; markers) {
		if (!markers.hasNext())
			return stringToReplace;
		String[] pair = tupla(markers.next());
		String newString = stringToReplace.replace(name(pair), value(pair));
		return replaceEmptyMarkers(replace(newString, markers));
	}

	private String replaceEmptyMarkers(String replace) {
		return replace.replaceAll("\\$\\w+", "");
	}

	private String value(String[] pair) {
		return (pair.length != 2) ? "" : pair[1].trim();
	}

	private String name(String[] pair) {
		if (pair.length != 2)
			return "";
		return "$" + pair[0].trim();
	}
}</pre>
<h3>Conclusions</h3>
<p><a href="http://www.jdave.org/">JDave</a> is very easy to learn and the specifications written throught it are very complete and expressive.<br />
If I have to make a choice, I liked slightly more <a href="http://www.jdave.org/">JDave</a>, but I think are just two tools: BDD is a way to think, not a framework or an engine to use.</p>
<p>In conclusions, I believe <a href="http://jbehave.org/">JBehave </a>and <a href="http://www.jdave.org/">JDave</a> could be used together, the former to describe the user stories, at user level, the latter to describe the behaviors of the classes, at developer level.</p>

<!-- start wp-tags-to-technorati 1.01 -->

<p class='technorati-tags'>Technorati Tags: <a class='technorati-link' href='http://technorati.com/tag/bdd' rel='tag' target='_self'>bdd</a>, <a class='technorati-link' href='http://technorati.com/tag/eclipse' rel='tag' target='_self'>eclipse</a>, <a class='technorati-link' href='http://technorati.com/tag/java' rel='tag' target='_self'>java</a>, <a class='technorati-link' href='http://technorati.com/tag/jdave' rel='tag' target='_self'>jdave</a></p>

<!-- end wp-tags-to-technorati -->
]]></content:encoded>
			<wfw:commentRss>http://giordano.scalzo.biz/2009/09/17/using-jdave-a-quick-introduction-to-specs-framework/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
