<?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>Shaaf's Blog &#187; jbehave</title>
	<atom:link href="http://www.shaafshah.com/tag/jbehave/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.shaafshah.com</link>
	<description>Another bit in the wall</description>
	<lastBuildDate>Wed, 12 Aug 2009 14:28:51 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Getting started with JBehave in 8 steps.</title>
		<link>http://www.shaafshah.com/2009/08/12/getting-started-with-jbehave-in-8-steps/</link>
		<comments>http://www.shaafshah.com/2009/08/12/getting-started-with-jbehave-in-8-steps/#comments</comments>
		<pubDate>Wed, 12 Aug 2009 14:13:30 +0000</pubDate>
		<dc:creator>Shaaf Shah</dc:creator>
				<category><![CDATA[HOWTO]]></category>
		<category><![CDATA[Software Development]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[jbehave]]></category>
		<category><![CDATA[unit testing]]></category>

		<guid isPermaLink="false">http://www.shaafshah.com/?p=256</guid>
		<description><![CDATA[This post is about JBehave and how to quickly get started with it. If you would like to know about BDD please use the following link. What is Behavioral Driven Development? Today I have used JBehave for the first time. It does have some convincing factors for instance diving requirements into scenarios which map pretty [...]]]></description>
			<content:encoded><![CDATA[<p>This post is about JBehave and how to quickly get started with it. If you would like to know about BDD please use the following link.<br />
<a href="http://en.wikipedia.org/wiki/Behavior_Driven_Development">What is Behavioral Driven Development?</a></p>
<p>Today I have used JBehave for the first time. It does have some convincing factors for instance diving requirements into scenarios which map pretty nicely to the tests that are written with in the Steps. Thus it seems like it would be easier for Stakeholder/s to use it as a good guideline for the initial requirements. Its always quite usual to come up to some disagreements about the development however the tool does help to bring forth the ease for stake holders who really dont have to get into writing code but will have a technical jargon to communicate through to the developers in shape of scenarios.</p>
<p>So the first things come up.<br />
1. Create a Java project in Eclipse.<br />
2. Download the JBehave latest version from the <a href="http://jbehave.org/software/download/">website</a> and add the jar files to your project build classpath<br />
3. Create a scenario file and name it as user_wants_to_shop.scenario</p>
<p>Add the following text to the file</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code"><pre class="java" style="font-family:monospace;">Given user Shaaf is logged in
Check <span style="color: #000000; font-weight: bold;">if</span> user shaaf is Allowed to shop
Then show shaaf his shopping cart</pre></td></tr></table></div>

<p>The lines above simply state the rules for the scenario we are about to create.</p>
<p>4. Now we will create a java file mapping to it.</p>
<p>Create a new class with the name corresponding to the same as the .scenario file<br />
UserWantsToShop.java</p>
<p>Add the following code to it</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.jbehave.scenario.PropertyBasedConfiguration</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.jbehave.scenario.Scenario</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.jbehave.scenario.parser.ClasspathScenarioDefiner</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.jbehave.scenario.parser.PatternScenarioParser</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.jbehave.scenario.parser.ScenarioDefiner</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.jbehave.scenario.parser.UnderscoredCamelCaseResolver</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> UserWantsToShop <span style="color: #000000; font-weight: bold;">extends</span> Scenario <span style="color: #009900;">&#123;</span>
&nbsp;
        <span style="color: #666666; font-style: italic;">// The usual constructor with default Configurations.</span>
	<span style="color: #000000; font-weight: bold;">public</span> UserWantsToShop<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">super</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> ShoppingSteps<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">// Mapping .scenario files to the scenario. This is not by default.</span>
    <span style="color: #000000; font-weight: bold;">public</span> UserWantsToShop<span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">final</span> <span style="color: #003399;">ClassLoader</span> classLoader<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">super</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> PropertyBasedConfiguration<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000000; font-weight: bold;">public</span> ScenarioDefiner forDefiningScenarios<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000000; font-weight: bold;">new</span> ClasspathScenarioDefiner<span style="color: #009900;">&#40;</span>
                    <span style="color: #000000; font-weight: bold;">new</span> UnderscoredCamelCaseResolver<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;.scenario&quot;</span><span style="color: #009900;">&#41;</span>, 
                    <span style="color: #000000; font-weight: bold;">new</span> PatternScenarioParser<span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>, classLoader<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
        <span style="color: #009900;">&#125;</span>, <span style="color: #000000; font-weight: bold;">new</span> ShoppingSteps<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>5. Just that we now have steps we need some place to put the logic for the Steps in the Scenarios. So we create a new Steps class ShoppingSteps.java</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.jbehave.scenario.annotations.Given</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.jbehave.scenario.annotations.Then</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.jbehave.scenario.annotations.When</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.jbehave.scenario.steps.Steps</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> ShoppingSteps <span style="color: #000000; font-weight: bold;">extends</span> Steps <span style="color: #009900;">&#123;</span>
&nbsp;
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>6. Now you can run the Scenario by running it as a Junit Test.</p>
<p>Following should be the output</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code"><pre class="java" style="font-family:monospace;">Scenario<span style="color: #339933;">:</span> 
&nbsp;
Given I am not logged in <span style="color: #009900;">&#40;</span>PENDING<span style="color: #009900;">&#41;</span>
When I log in as Shaaf with a password JBehaver <span style="color: #009900;">&#40;</span>PENDING<span style="color: #009900;">&#41;</span>
Then I should see my <span style="color: #0000ff;">&quot;Shopping Cart&quot;</span> <span style="color: #009900;">&#40;</span>PENDING<span style="color: #009900;">&#41;</span></pre></td></tr></table></div>

<p>This means that none of the scenario requirements have been implemented as yet. But the test result should be green to show there are no problems with our setup.</p>
<p>Now lets add the implementation to the tests.</p>
<p>7. Add the body to the ShoppingSteps. I have added the comments with the methods.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
</pre></td><td class="code"><pre class="java" style="font-family:monospace;">        <span style="color: #666666; font-style: italic;">// Given that a user(param) is loggen in</span>
	@Given<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;user $username is logged in&quot;</span><span style="color: #009900;">&#41;</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> logIn<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> userName<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		checkInSession<span style="color: #009900;">&#40;</span>userName<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #666666; font-style: italic;">// Check if the user is allowed on the server</span>
	@When<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;if user $username is $permission to shop&quot;</span><span style="color: #009900;">&#41;</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> isUserAllowed<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> userName, <span style="color: #003399;">String</span> permission<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		getUser<span style="color: #009900;">&#40;</span>userName<span style="color: #009900;">&#41;</span>.<span style="color: #006633;">isUserAllowed</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">equals</span><span style="color: #009900;">&#40;</span>permission<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
       <span style="color: #666666; font-style: italic;">// finally then let him use the shopping cart.</span>
	@Then<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;show $username his Shopping cart&quot;</span><span style="color: #009900;">&#41;</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> getMyCart<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> userName, <span style="color: #003399;">String</span> cart<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		getUserCart<span style="color: #009900;">&#40;</span>userName<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>8. Now you should try to run the scenario again. And all of the test should be green. I have not implemented the actual methods so they will be red. <img src='http://www.shaafshah.com/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://www.shaafshah.com/2009/08/12/getting-started-with-jbehave-in-8-steps/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
	</channel>
</rss>
