JBehave BDD – How to get Started using the Eclipse jBehave Plugin

PerfectoTopAd

Do not overrate what you have received, nor envy others. He who envies others does not obtain peace of mind. ~ Buddha

Do you suffer from SpecFlow Envy?

If you have worked in a .NET environment using BDD with the BDD tool Specflow and then switched to a project that used Java, you might be suffering from SpecFlow envy. Specflows integration with Visual Studio is awesome, and it does a lot of things behind the scenes automatically for you.

When you start working in a BDD Java environment you will most likely miss some of Specflow's features.

If you are using jBehave –envy no more – there's a jBehave plugin available for Eclipse that helps get some of the Specflow functionality that you crave.


JBehave Plugin Features

By integrating the jBehave plug-in with Eclipse, you get the following features:

  • Step auto-completion
  • Syntax highlighting
  • Step hyperlink detection and link to corresponding Java step method (Ctrl+click)
  • Step validation, detecting both unimplemented steps and ambiguous steps

You can also go into the jBehave preferences in Eclipse and change:

  • Syntax color themes
  • Story language
  • Console logger levels


For the following example to work, I'll assume you already have all the jBehave jar files added to your Libraries area in the project Java Build Path.


How to install the Eclipse JBehave Plug-In

  1. In Eclipse, go to Help>Install New Software
  2. Click the Add button
  3. In the Add Repository dialog, enter:
    1. Name: JBehave
    2. Location: jBehave – http://jbehave.org/reference/eclipse/updates/
  4. Under the Available Software dialog, click on the jBehave option


  1. Click Next>
  2. Take defaults for the rest of the prompts
  3. Restart Eclipse

JBehave New Story Wizard

To verify that the plugin installed, when you right click on a project and select New>Other you should now have an option for a jBehave>New Story wizard.


Quick Example – jBehave Plugin in Action

The best way to get started with the jbehave plugin is to create a quick, simple, project to become familiar with how it works. So let's create a new project in Eclipse and take a look.

Create a new project

  1. In Eclipse, Create a New>Java Project
  2. In the New Java Project dialog, enter the project name jBehavePlugIn
  3. Click Finish
  4. Create two new Packages named:
    1. com.joe.steps
    2. com.joe.features * jBehave calls features—stories. To stay consistent with Gerkin and Cucumber standards we will instead use the term features.


Create a jBehave story

  1. Right click on the com.joe.features package and select New>Other and the JBehave>New story wizard
  2. In the Story dialog, name the story File name: debugPlugIn.feature


  1. Click Finish
  2. You should now have a debugPlugIn.feature file that looks like this in the IDE:


Notice how all the Given, When and Then steps have a yellow triangle with an exclamation point next to them. This shows that there are currently no steps in the project that match the Given, When or Then statements. Clicking on one of the triangles would show a No step is matching message:


If you clicked on one of the Scenario statements using Ctrl+Click, you would get the following message:


Create a Steps Class

Let's create a step class and add a step definition for the first Given statement.

  • Right click on the com.joe.steps package and select New>Class
  • In the New Java Class enter the Name: debugPluginSteps
  • Click Finish
  • Open the debugPluginSteps.java class and add the following import statements:
    • import org.jbehave.core.annotations.Given;
    • import org.jbehave.core.annotations.Then;
    • import org.jbehave.core.annotations.When;
  • Create the following Given method using the first Given step
    step represents a precondition to an event that we have in our debugPlugIn.feature file:
@Given("step represents a precondition to an event")
public void debugStepGivenTest(){

}


Save the project and return to the debugPlugin.feature file. Notice how the first Given step represents a precondition to an event no longer has a yellow triangle next to it. Hold the control key down on your keyboard and click on the Given step represents line, and – check it out – it takes you directly to its corresponding Java step!

jBehave Plugin Wrap-up

That's it. It may not seem like much, but this will be a huge time saver; especially when you start having lots of features and lots of steps. It's also great to get a visual look at a feature file and be able to automatically see which scenarios still need step definition code defined.

8 comments
Deepak Jain - December 10, 2013

We need more tutorial like this which shows
-relation between schearios and steps.
– writing complex @Given, @when and @Then

Reply
    Joe Colantonio - December 11, 2013

    Thanks Deepak –I do plan on writing more BDD post in the next few months. So stay tuned

    Reply
william - March 11, 2014

Very good for the beginner of JBehave. But why the executing class is not included?
Thanks.

Reply
Astha - December 17, 2014

Hi there,

This is very nice article, Please also let us know that what jars files are needed to add, because i ab only able to find out one .jar file (jbehave-core-3.6.8.jar), Also do mention the dependencies needs to add to a maven project.

Also provide the code for running class.

Thanks!

Reply
Dustin - January 7, 2015

The imports don’t work. There is a red wavy line under “org.jbehave.core” part of the import. Has anything changed?

Reply
    Joe Colantonio - January 13, 2015

    Did you download the jbehave jar files and add them to your project under the java build path>Libraries tab?

    Reply
Manish Chauhan - January 5, 2016

Why Execution class is not included in the post for complete flow and running.

Reply
anji - March 12, 2017

Thanks for your valuable information in this tutorial. If you could not able to post you please send the code to my E-mail id. This will be a great help for me. Could you please develop a JBehave _Selenium script for login functionality and script for running with JUnit.

Reply
Click here to add a comment

Leave a comment: