I have been wondering for a while, if there was a way to get all my BDD steps in one place to so that I can do a search on the existing steps. As the Library of steps grows gradually often finding existing steps becomes a lazy and tedious activity. And then we end up with multiple steps doing same things.

Jbehave reports can be ebabled to show something which is called Storynavigator. Its a client side javascript library which enabled browser to read meta data generated by cross referencing in Jbehave. Cross referencing can be enabled very easily from the configuration and it then generated the meta data which can be used by this story navigator.

How to enable cross referencing in Jbehave configuration?

 @Override
    public Configuration configuration() {
    	CrossReference xref = new CrossReference();
 
        Class embeddableClass = this.getClass();
        // Start from default ParameterConverters instance
        ParameterConverters parameterConverters = new ParameterConverters();
        // factory to allow parameter conversion and loading from external resources (used by StoryParser too)
        ExamplesTableFactory examplesTableFactory = new ExamplesTableFactory(new LocalizedKeywords(), new LoadFromClasspath(embeddableClass), parameterConverters);
        // add custom coverters
        parameterConverters.addConverters(new DateConverter(new SimpleDateFormat("yyyy-MM-dd")),
                new ExamplesTableConverter(examplesTableFactory));
        parameterConverters.addConverters(new MyConverter());
        parameterConverters.addConverters(new StudentConverter(new ExamplesTableFactory()));
 
		return new MostUsefulConfiguration()
            .useStoryLoader(new LoadFromClasspath(embeddableClass))
            .useStoryParser(new RegexStoryParser(examplesTableFactory)) 
            .useStoryReporterBuilder(new StoryReporterBuilder()
                .withCodeLocation(CodeLocations.codeLocationFromClass(embeddableClass))
                .withDefaultFormats()
                .withFormats(CONSOLE, TXT, HTML, XML)
                .withCrossReference(xref))
            .useParameterConverters(parameterConverters)    
            .useStepMonitor(xref.getStepMonitor());
    }

Now the next step is to copy the JS libraries in your project and make sure they are copied in the view directory. Download the all the files and folder from here https://github.com/jbehave/jbehave-core/tree/master/jbehave-navigator/src/main/resources. Copy them under src/main/storynavigator folder in your source.

Add this confif in your POM files, so that these files are copied in the Jbehave view directory

        ${basedir}/src/main/storynavigator
        ${project.build.directory}/jbehave/view
        false

Now every run will generate a file called navigator.html inside /target/jbehave/view.

First tab is Stories, it will allow searching on story description, narrative and meta tag. Second tab is the Steps Tab, this will allow searching all the steps in your step classes.

Bit of learning – You will not see data in Stories tab for you story if no meta tags are used in the report.

 

,

If you are using Jbehave and wonder how to execute your scenarios with different set of data without repeating the steps, so that your scenarios don’t blow up repeating same steps with different set of data.

You can also refer to this scenario as data driven testing, as each of the row in your data table becomes one test case.

In the example below lets say I am testing a calculator sum function, and for that I need to have several test cases to test with different data. So basically steps looks like this, however we need to give different input and assert output.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
 
 
Scenario: As as user I want to test calculator sum function 
Meta:
@author pankaj
 
Given calculator takes <input1> and <input2>
Then the sum is <sum>
 
Examples:
|input1|input2|sum|
|2|3|5|
|4|5|9|
|10|12|22|

And steps implementation for the above written steps will need to use Named parameter. So basically Named parameter (Which is usually in <>), will be replaced by the data in the table below the steps in the story. In the above example there are three named parameter in my story.



In the steps implementation, you step needs to explicitly use the named parameter in the method. Using named parameter in the steps tells jbehave that it needs to replace this with the data in the table specified below/or in another file.

Steps class would look like

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
package org.qainfolabs.jbehave.steps;
 
import java.math.BigDecimal;
 
import junit.framework.Assert;
 
import org.jbehave.core.annotations.Given;
import org.jbehave.core.annotations.Named;
import org.jbehave.core.annotations.Then;
import org.qainfolabs.app.Calculator;
 
public class CalulatorSteps {
 
	private BigDecimal input1;
	private BigDecimal input2;
 
	@Given("calculator takes <input1> and <input2>")
	public void readFruits(@Named("input1") BigDecimal input1,
			@Named("input2") BigDecimal input2) {
		this.input1 = input1;
		this.input2 = input2;
	}
 
	@Then("the sum is <sum>")
	public void doSum(@Named("sum") BigDecimal sum) {
		Calculator calc = new Calculator();
		Assert.assertEquals(sum, calc.sum(input1, input2));
	}
}

After the scenario is executed Jbehave will produce a detailed report of the scenario, showing the steps repeating with replaced parameter with each data set.

,

Behaviour driven development is now widely accepted process in Agile community. Now the question to be asked what is that make this simple discipline powerful and effective? And how to use the power of communication?

Agile development practices have been preaching for years on the aspect of writing acceptance tests for the requirements upfront. These acceptance tests used to be written in various and tools. Fitnesse definitely levraged among all other tools, simply because it enabled developement teams to use power of communication. No longer there was divide between tests being too technical for the people to read who actually cared about them, ofcourse I am talking about business , BAs, Testers.

Behaviour Driven Development is refined form for writing Acceptance tests, where requirements are expressed in natural language of communication (English, French etc) and implemented in the same form. This enabled no loss of communication from Requirements to Tests. And also it forces business to be involved with requirement throughout, as they have visibility on what they asked WORKS !

Tools like Cucumber, Concordian, Jbehave, Easyb enabled a uniform language of communication for Requirements and Tests , which looks like a simple discipline but it is very powerful.

Not only business gets lot of value from it at the same time development teams are equally benefited, as they have clear understanding of expected software behaviour in the form of executable requirement. Also, the power of BDD lies in thinking about business problem and expressing the intent without thinking about the implementation details of the problem.

A BDD can express the intent in form of Simple Sentences, Tables, Examples etc. The most common form of Acceptance testing format is Given – When – Then, which is widely being used in BDDs.

Given – Is a prerequisite or precondition for an action.
When – A action or Event happens.
Then – Results of the action.

Example – Saving bank account withdraw scenario

Given there are 1000$ in saving bank account number XXXXXXX
When a cash with drawl of $900 is done
Then available balance is $100

The above example is a simple explanation of How a Given When Then can express an intent in consistent and effective way.

,