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:
- Created new Java Project
- Downloaded JBehave JAR file version 3.6.8 and added it to my build path libraries
- Created a package called
com.wmi.tutorials.bdd.stack.specs
under the test source folder in my workspace - Added the JBehave JAR file to my Build path Library configuration
- Created a JBehave story in the above-mentioned package (StackBehaviourStories.story)
- Created a Java class in the above-mentioned package (StackBehaviourStory.java)
- Created a Java class in the above-mentioned package (StackBehaviourSteps.java)
- Imported the Given, Named, Then, When annotations in my Java class
- 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?
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:
ICanToggleACell.java
class, right-click it, then Run As, and click on JUnit TestSo 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.i had updated the JunitStory to JunitStories and it worked
public class StackBehaviourStory extends JUnitStory ---> JunitStories
I know I'm late to the party here but I'm posting because this is the info I wish I had a week ago as it would've saved me a lot of pain. I'm very much into the idea of BDD, but am unfortunately finding JBehave's docs to be a bit of a nightmare, especially when it comes to Maven integration. Moreover a lot of the code I found both on their website and elsewhere didn't work. Through trial and error, and lots of tutorials, I was able to piece together the following. It runs both in Maven and Eclipse, has a single binding class that maps stories to step files, and is able to find story files located in src/test/resources.
here is a working pom file:
here is a sample story file
here is a sample step file
and here is the generic runner
the runner lives in src/test/java//tests.runner. the ingest test lives in src/test/java//tests.ingest. the story files live in src/test/resources/stories.
As far as I can tell, JBehave has LOTS of options, so this certainly isn't the only way of doing things. Treat this like a template that will get you up and running quickly.
full source is on github.
In my case, I have extended my Steps class from Steps (from jbehave core)