Up and running with: JBehave

This is the fourth article in our series on new, popular or otherwise interesting tools used in test automation. You can read all posts within this series by clicking here.

What is JBehave?
From the JBehave.org website: JBehave is a framework for Behaviour-Driven Development (BDD). BDD is an evolution of test-driven development (TDD) and acceptance-test driven design, and is intended to make these practices more accessible and intuitive to newcomers and experts alike. It shifts the vocabulary from being test-based to behaviour-based, and positions itself as a design philosophy.

Using JBehave follows these five simple steps, which we will follow in the examples provided in this article as well:

  1. Write your user story
  2. Map the steps in the user story to Java code
  3. Configure your user stories
  4. Run your JBehave tests
  5. Review the test results

Where can I get JBehave?
JBehave can be downloaded from this site.

How do I install and configure JBehave?
JBehave consists of a number of .jar files. All you need to do is to add these .jar files as a dependency to your Java project and you’re ready to start using JBehave. You can also set up and configure JBehave using Maven. See for more details the extensive JBehave reference guide here.

Creating a first JBehave user story and corresponding test
Before we start writing our first JBehave test, we need a Java class that we can test. For this purpose, I have written a very simple POJO class Flipper with two variables: a String state (which is either ‘even’ or ‘odd’) and an Integer value. The source code for this class is included in the download at the end of this post. Among the methods for this class is a method flipState, which flips the state from ‘even’ to ‘odd’ (or the other way round) and increases the value by 1. Useless, maybe, but it does the trick for this example!

Step 1: Write your user story
A user story is written in the ‘given-when-then’ format that is characteristic for the BDD approach. In our example, we want to make sure (i.e., test) that given a fresh instance of the Flipper class, when we call the flipState method, then the value variable of the Flipper class is assigned the value 2. See how I used the ‘given-when-then’ strcuture to describe my test case? Translated to a user story that is JBehave-readable, our test case looks like this:

Scenario: when a user flips a flipper, its value is increased by 1

Given a flipper
Given the flipper has value 1
When the user flips the flipper
Then the value of the flipper must become 2

Step 2: Map the steps in the user story to Java code
One of the nicer features of JBehave is that you can run your tests as JUnit tests, which makes for easy integration with the rest of your testing and / or continuous integration framework. To do this, your test class containing the implementation of your story steps should extend the JUnitStories class. JBehave uses the @Given, @When and @Then annotations to identify story steps and their nature. For example, our test class and the implementation of the first ‘given’ step might look like this:

public class JBehaveTest extends JUnitStories {

	private Flipper flipper;

	@Given("a flipper")
	public void aFlipper() {

		flipper = new Flipper();

Now, whenever JBehave encounters the line ‘Given a flipper’ in one of the stories that is executed, it will automatically execute the aFlipper() method.

We can do the same for the ‘when’ and ‘then’ clauses of our story, using the appropriate annotations:

@When("the user flips the flipper")
public void whenTheUserFlipsTheFlipper() {


@Then("the value of the flipper must become 2")
public void valueOfFlipperMustBecomeTwo() {

	Assert.assertEquals(2, flipper.getValue());

Step 3: Configure your user stories
In order for our stories to be run, we need to tell JBehave where our stories are located, and which configuration should be used to execute them. For now, we are going to use the default configuration.

public Configuration configuration() {
	return new MostUsefulConfiguration().useStoryLoader(new LoadFromClasspath(getClass().getClassLoader())).useStoryReporterBuilder(new StoryReporterBuilder().withFormats(Format.CONSOLE));

public List<CandidateSteps> candidateSteps() {
	return new InstanceStepsFactory(configuration(), this).createCandidateSteps();
protected List<String> storyPaths() {
	return Arrays.asList("com/ontestautomation/jbehave/demo/test_value.story");

public void run() throws Throwable {

The configuration method loads the default configuration for running our JBehave tests, known as the MostUsefulConfiguration. The candidateSteps method loads the story steps defined in the current class (‘this’). The storyPaths method is used to define which stories are going to be executed. I included my sample story in the Eclipse project I used, but you can load story definitions from everywhere, including from an URL. Finally, the run method allows us to run our class as a test.

Step 4: Run your JBehave tests
Since we defined our JBehave tests as JUnit tests, running them is as easy as running regular JUnit tests. For example, in Eclipse, you can just right-click on your JBehave test class and select Run As > JUnit Test.

Step 5: Review the test results
Again, since our JBehave tests are just another type of JUnit tests, we can review the results for our tests like we do for any other JUnit test. Again, in Eclipse, a new tab displaying our test results opens automatically after test execution:
JBehave test results
In the Eclipse console, we can also see that our story has been processed successfully:

Processing system properties {}
Using controls EmbedderControls[batch=false,skip=false,generateViewAfterStories=true,ignoreFailureInStories=false,ignoreFailureInView=false,verboseFailures=false,verboseFiltering=false,storyTimeoutInSecs=300,threads=1]


Running story com/ontestautomation/jbehave/demo/test_value.story

Scenario: when a user flips a flipper, its value is increased by 1
Given a flipper
Given the flipper has value 1
When the user flips the flipper
Then the value of the flipper must become 2

Useful features
JBehave offers a lot of useful features, all of which are described in the online reference manual which can be found here. Some of the most notable features are:

Further reading
An Eclipse project including my Flipper class implementation and the JBehave test story I’ve demonstrated above can be downloaded here.

Happy JBehaving!