Search code examples
javaeclipsejunitbddjbehave

Very simple step by step JBehave setup tutorial?


Though I have read many, but many articles on how to use JBehave, I can't get it to work. Here are the steps I went through so far:

  1. Created new Java Project
  2. Downloaded JBehave JAR file version 3.6.8 and added it to my build path libraries
  3. Created a package called com.wmi.tutorials.bdd.stack.specs under the test source folder in my workspace
  4. Added the JBehave JAR file to my Build path Library configuration
  5. Created a JBehave story in the above-mentioned package (StackBehaviourStories.story)
  6. Created a Java class in the above-mentioned package (StackBehaviourStory.java)
  7. Created a Java class in the above-mentioned package (StackBehaviourSteps.java)
  8. Imported the Given, Named, Then, When annotations in my Java class
  9. Written two different scenarios in my JBehave story file

And still, I can't get it to work/run! =(

The story file:

Narrative:
In order to learn to with JBehave using Eclipse
As a junior Java developer though senior in .Net and in BDD
I want to define the behaviour of a custom stack

Scenario: I push an item onto the stack
Given I have an empty stack
When  I push an item 'orange'
Then  I should count 1

Scenario: I pop from the stack
Given I have an empty stack
When  I push an item 'apple'
And   I pop the stack
Then  I should count 0

The story class

package com.wmi.tutorials.bdd.stack.specs

import org.jbehave.core.configuration.MostUsefulConfiguration;
import org.jbehave.core.junit.JUnitStory;

public class StackBehaviourStory extends JUnitStory {
    @Override 
    public Configuration configuration() { return new MostUsefulConfiguration(); }

    @Override
    public InjectableStepsFactory stepsFactory() {
        return new InstanceStepsFactory(configuration()
                                      , new StackBehaviourSteps());   
    }
}

The steps class

package com.wmi.tutorials.bdd.stack.specs

import org.jbehave.core.annotations.Given;
import org.jbehave.core.annotations.Named;
import org.jbehave.core.annotations.Then;
import org.jbehave.core.annotations.When;
import org.jbehave.core.junit.Assert;

public class StackBehaviourSteps {
    @Given("I have an empty stack")
    public void givenIHaveAnEmptyStack() { stack = new CustomStack(); }

    @When("I push an item $item")
    public void whenIPushAnItem(@Named("item") String item) { stack.push(item); }

    @Then("I should count $expected")
    public void thenIShouldCount(@Named("expected") int expected) {
        int actual = stack.count();
        if (actual != expected) 
            throw new RuntimeException("expected:"+expected+";actual:"+actual);
    }
}

I'm currently using Eclipse Kepler (4.3) JEE with everything I need to use JUnit, Google App Engine, and yes, JBehave is installed correctly following the Eclipse JBehave installation tutorial.

I can't get it to work. So how can I make it work correctly using Eclipse, JBehave and JUnit?


Solution

  • Following step by step closely the jbehave Getting Started tutorial, the Run story section says: [...] the ICanToggleACell.java class will allow itself to run as a JUnit test.

    This means that the JUnit library is required in your Build path.

    Using Eclipse:

    1. Select your current project and right-click it, Build path, Configure Build Path...
    2. Properties for [current project], Java Build Path, Libraries, click [Add Library...]
    3. Add Library, select JUnit, click [Next]
    4. JUnit Library, JUnit library version, select the version you wish to use, click [Finish]
    5. Java Build Path, click [OK]
    6. Project Explorer, select your ICanToggleACell.java class, right-click it, then Run As, and click on JUnit Test

    So this is the same here as for the above-example code. The StackBehaviourStory.java class should let itself run as a JUnit test after you add the proper library to the Java build path.