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
- In Eclipse, go to Help>Install New Software
- Click the Add button
- In the Add Repository dialog, enter:
- Name: JBehave
- Location: jBehave – http://jbehave.org/reference/eclipse/updates/
- Name: JBehave
- Under the Available Software dialog, click on the jBehave option
- Click Next>
- Take defaults for the rest of the prompts
- 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
- In Eclipse, Create a New>Java Project
- In the New Java Project dialog, enter the project name jBehavePlugIn
- Click Finish
- Create two new Packages named:
- com.joe.steps
- com.joe.features * jBehave calls features—stories. To stay consistent with Gerkin and Cucumber standards we will instead use the term features.
- com.joe.steps
Create a jBehave story
- Right click on the com.joe.features package and select New>Other and the JBehave>New story wizard
- In the Story dialog, name the story File name: debugPlugIn.feature
- Click Finish
- 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;
- import org.jbehave.core.annotations.Given;
- 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.
We need more tutorial like this which shows
-relation between schearios and steps.
– writing complex @Given, @when and @Then
Thanks Deepak –I do plan on writing more BDD post in the next few months. So stay tuned
Very good for the beginner of JBehave. But why the executing class is not included?
Thanks.
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!
The imports don’t work. There is a red wavy line under “org.jbehave.core” part of the import. Has anything changed?
Did you download the jbehave jar files and add them to your project under the java build path>Libraries tab?
Why Execution class is not included in the post for complete flow and running.
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.