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
UserWantsToShop.java

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;
  7.  
  8. public class UserWantsToShop extends Scenario {
  9.  
  10.         // The usual constructor with default Configurations.
  11. 	public UserWantsToShop(){
  12. 		super(new ShoppingSteps());
  13. 	}
  14.  
  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.     }
  25.  
  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 ShoppingSteps.java

  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;
  5.  
  6. public class ShoppingSteps extends Steps {
  7.  
  8. }

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

Following should be the output

  1. Scenario: 
  2.  
  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. 	}
  6.  
  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. 	}
  12.  
  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. 🙂

Logging with log4J isDebugEnabled

Alot of times I have seen the questions popping up whether to use isDebugEnabled property or not. Arguably most of the times or rather always about performance. Some of the stuff that I feel important about using it follows.

The answer is simple. It is made to be used. However using it has to be with caution.

For instance
If I am using the following line in my code.

  1. log.debug("I am there");

Thats an example of a good practise

However I can really blow it out of proportions if I do the following.

  1. // Not good practise
  2. if (log.isDebugEnabled()){
  3.     log.debug("I am there"); 
  4. }

And why exactly is that so. This is because the method debug in the class Category i.e. extended by Logger in the log4j libraries explicitly checks the mode for the logging itself.

Extract from the class org.apache.log4j.Category is below

  1. public void debug(Object message) {
  2. if(repository.isDisabled(Level.DEBUG_INT))
  3. return;
  4. if(Level.DEBUG.isGreaterOrEqual(this.getEffectiveLevel())) {
  5. forcedLog(FQCN, Level.DEBUG, message, null);
  6. }
  7. }

Okay so thats the case where we say dont use it. What about the one where we actually do use the isDebugEnabled property.

For instance you have a large parameter going in the debug.

  1. // Bad Practise
  2.  log.debug("XML "+Tree.getXMLText());

In such a case it can clearly be said “No dont do it!” If Tree.getXMLText is going to pull out all the hair out of the app then there is no use. As it will be constructed first and if the debug level is not enabled that would be a performance cost.

  1. // Good Practise
  2. if (log.isDebugEnabled()){
  3.     log.debug("XML "+Tree.getXMLText());
  4. }

Thus in the example above it would be wiser to use log.isDebugEnabled() just to make sure the mileage or cost stays lesser.