Getting started with JBehave in 8 steps.

This post is about JBehave and how to quickly get started with it. If you would like to know about BDD please use the following link.
What is Behavioral Driven Development?

Today I have used JBehave for the first time. It does have some convincing factors for instance diving requirements into scenarios which map pretty nicely to the tests that are written with in the Steps. Thus it seems like it would be easier for Stakeholder/s to use it as a good guideline for the initial requirements. Its always quite usual to come up to some disagreements about the development however the tool does help to bring forth the ease for stake holders who really dont have to get into writing code but will have a technical jargon to communicate through to the developers in shape of scenarios.

So the first things come up.
1. Create a Java project in Eclipse.
2. Download the JBehave latest version from the website and add the jar files to your project build classpath
3. Create a scenario file and name it as user_wants_to_shop.scenario

Add the following text to the file

  1. Given user Shaaf is logged in
  2. Check if user shaaf is Allowed to shop
  3. Then show shaaf his shopping cart

The lines above simply state the rules for the scenario we are about to create.

4. Now we will create a java file mapping to it.

Create a new class with the name corresponding to the same as the .scenario file

Add the following code to it

  1. import org.jbehave.scenario.PropertyBasedConfiguration;
  2. import org.jbehave.scenario.Scenario;
  3. import org.jbehave.scenario.parser.ClasspathScenarioDefiner;
  4. import org.jbehave.scenario.parser.PatternScenarioParser;
  5. import org.jbehave.scenario.parser.ScenarioDefiner;
  6. import org.jbehave.scenario.parser.UnderscoredCamelCaseResolver;
  8. public class UserWantsToShop extends Scenario {
  10.         // The usual constructor with default Configurations.
  11. 	public UserWantsToShop(){
  12. 		super(new ShoppingSteps());
  13. 	}
  15. 	// Mapping .scenario files to the scenario. This is not by default.
  16.     public UserWantsToShop(final ClassLoader classLoader) {
  17.         super(new PropertyBasedConfiguration() {
  18.             public ScenarioDefiner forDefiningScenarios() {
  19.                 return new ClasspathScenarioDefiner(
  20.                     new UnderscoredCamelCaseResolver(".scenario"), 
  21.                     new PatternScenarioParser(this), classLoader);
  22.             }
  23.         }, new ShoppingSteps());
  24.     }
  26. }

5. Just that we now have steps we need some place to put the logic for the Steps in the Scenarios. So we create a new Steps class

  1. import org.jbehave.scenario.annotations.Given;
  2. import org.jbehave.scenario.annotations.Then;
  3. import org.jbehave.scenario.annotations.When;
  4. import org.jbehave.scenario.steps.Steps;
  6. public class ShoppingSteps extends Steps {
  8. }

6. Now you can run the Scenario by running it as a Junit Test.

Following should be the output

  1. Scenario: 
  3. Given I am not logged in (PENDING)
  4. When I log in as Shaaf with a password JBehaver (PENDING)
  5. Then I should see my "Shopping Cart" (PENDING)

This means that none of the scenario requirements have been implemented as yet. But the test result should be green to show there are no problems with our setup.

Now lets add the implementation to the tests.

7. Add the body to the ShoppingSteps. I have added the comments with the methods.

  1.         // Given that a user(param) is loggen in
  2. 	@Given("user $username is logged in")
  3. 	public void logIn(String userName){
  4. 		checkInSession(userName);
  5. 	}
  7.         // Check if the user is allowed on the server
  8. 	@When("if user $username is $permission to shop")
  9. 	public void isUserAllowed(String userName, String permission){
  10. 		getUser(userName).isUserAllowed().equals(permission);
  11. 	}
  13.        // finally then let him use the shopping cart.
  14. 	@Then("show $username his Shopping cart")
  15. 	public void getMyCart(String userName, String cart){
  16. 		getUserCart(userName);
  17. 	}

8. Now you should try to run the scenario again. And all of the test should be green. I have not implemented the actual methods so they will be red. 🙂

7 thoughts on “Getting started with JBehave in 8 steps.

  1. Shaaf,
    I followed your tutorial, but after I wrote the empty classes, when running as Junit test I get the following error:

    org.jbehave.scenario.errors.ScenarioNotFoundException: Scenario user_wants_to_shop could not be found by classloader sun.misc.Launcher$AppClassLoader@11b86e7
    at org.jbehave.scenario.parser.ClasspathScenarioDefiner.loadInputStreamFor(
    at org.jbehave.scenario.parser.ClasspathScenarioDefiner.loadScenarioDefinitionsFor(
    at org.jbehave.scenario.AbstractScenario.runScenario(
    at org.jbehave.scenario.JUnitScenario.runScenario(
    at org.jbehave.scenario.JUnitScenario.testScenario(
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(
    at java.lang.reflect.Method.invoke(
    at junit.framework.TestCase.runTest(
    at junit.framework.TestCase.runBare(
    at junit.framework.TestResult$1.protect(
    at junit.framework.TestResult.runProtected(
    at junit.framework.TestSuite.runTest(

    the file is, correctly, in bin directory with the classes.

    Any clue?

    1. Yep, It cannot find the file. I presume you have in a package? The scenario and the .java have to be on the same location. Thus if the .java is in a package so should be the .scenario.

  2. Hello Shaaf,

    thanks for that great tutorial! I had the same problem like Giordano and don’t know, if I got you right with your comment. I had to delete the extension .scenario from the file name and than it worked.

    Perhaps someone else also has that problem and this helps..

    Thanks and best regards,


  3. Hello Shaaf,
    Thanks for the tutorial. I had the same problem like Giordano and Dirk. And I did the same as Dirk, delete the .scenario extension from the scenario file.
    As per that, I realized the the UserWantsToShop method is not required cause we dont have files with .scenario to map. So I deleted that method to, and the program is still working.
    So the, would be something like;

    import org.jbehave.scenario.Scenario;

    public class UserWantsToShop extends Scenario {

    // The usual constructor with default Configurations.
    public UserWantsToShop(){
    super(new ShoppingSteps());

Leave a Reply

Your email address will not be published. Required fields are marked *