An Introduction to JBehave and BDD
Want to avoid a glassy-eyed stare you get when trying to explain your code to stakeholders? Check out how JBehave and behavior-driven development can help.
Join the DZone community and get the full member experience.
Join For FreeTesting is an important part of any project, and there are various types of testing available. JBehave is a nice tool for behavior-driven development, which actually tests from the stakeholder perspective.
If you have participated in client/user meetings to measure requirements, in the waterfall model, for example, or a Sprint planning meeting in Agile, you're aware of the importance of stakeholder perspective testing. Each piece of a task is known as a story, and BDD provides the provision to test these stories.
JBehave supports Java-based development, and plain English is used to form the story. The story comprises of one or more scenarios, and a scenario is made up of one or more steps.
There are three kind of steps — 'Given', 'When', and 'Then', which are used in all behavior-driven development tools.
The keywords in JBehave are:
'Scenario', 'GivenStories', 'Given', 'When', 'Then', 'And'
Now let's look at a working example uisng Eclipse IDE.
Required JARs
jbehave-core-3.8.jar
junit-4.10.jar
org.apache.commons.io.jar
commons-lang-2.4.jar
plexus-utils-1.1.jar
paranamer-2.5.jar
freemarker-2.3.9.jar
org.apache.commons.collections.jar
Eclipse Plugin
We're going to use the JBehave plugin for our project.
Help -> Install New Software.
Add the details. For simplicity, here's the URL: http://jbehave.org/reference/eclipse/updates/
Follow the instructions, and JBehave's Eclipse plugin will be installed. Restart Eclipse, and it's ready to write and execute the JBehave's test cases.
Verify JBehave Installed Correctly
File -> New -> Other
And you can see the JBehave in the wizard as below:
Now, your project structure should look like:
First, let's create the SUT (system under test) code. For our purposes, we'll used the MyLinkedList class below:
package com.bdd.linkedlist;
public class MyLinkedList {
Node head;
Node tail;
public void addNodeInLast(Node node){
if(null == tail){
head=node;
tail=node;
}else{
tail.setNext(node);
tail=node;
}
}
public int getSize(){
return traverse();
}
public int traverse(){
int listSize =0;
if(head == null){
return listSize;
}
else{
Node tempHead = head;
while(tempHead != null){
System.out.print("Node : "+tempHead.toString());
System.out.println(", ");
tempHead = tempHead.getNext();
listSize++;
}
System.out.println("");
}
return listSize;
}
}
MyLinkedList class defines the Node class as:
package com.bdd.linkedlist;
public class Node {
private String data;
private Node next;
public String getData() {
return data;
}
public void setData(String data) {
this.data = data;
}
public Node getNext() {
return next;
}
public void setNext(Node next) {
this.next = next;
}
public Node(String data, Node next){
this.data= data;
this.next= next;
}
public String toString(){
return data;
}
}
Now my SUT is ready, and we need JBehave test cases to test the added functionality of MyLinkedList.
We create the story file in the interest of checking the business test cases. In this case, we'll look at add functionalty.
My_linked_list_story.story
Narrative:
In order to communicate effectively to the business some functionality
As a development team
I want to use Behaviour-Driven Development
Scenario: My LinkedList under test for adding the nodes
Given 5 nodes
When node value as "test-1"
And add them in the list
And node value as "test-2"
And add them in the list
And node value as "test-3"
And add them in the list
And node value as "test-4"
And add them in the list
And node value as "test-5"
And add them in the list
Then get the size as 5
Now create the Step definition for the above story file:
MyLinkedListSteps.java
package com.bdd.jbehave;
import org.jbehave.core.annotations.Given;
import org.jbehave.core.annotations.Then;
import org.jbehave.core.annotations.When;
import com.bdd.linkedlist.MyLinkedList;
import com.bdd.linkedlist.Node;
import junit.framework.Assert;
public class MyLinkedListSteps {
private int numOfNodes ;
private String nodeVal;
private MyLinkedList myLinkedList;
@Given("$nnum nodes")
public void numOfNodes(int num) {
this.numOfNodes = num;
myLinkedList = new MyLinkedList();
System.out.println("Number of nodes going to add in List ===> "+ num);
}
@When("node value as $val")
public void addNodeValues(String val) {
System.out.println(">>>> adding the values in the list as -- "+ val);
this.nodeVal = val;
}
@When("add them in the list")
public void addThemInList() {
System.out.println(">>>> Adding into the list...");
myLinkedList.addNodeInLast(new Node(this.nodeVal, null));
}
@Then("get the size as $size")
public void getTheListSize(int size) {
System.out.println(">>>> Expected List size is - "+size);
Assert.assertTrue(size == myLinkedList.getSize());
}
}
Now let's have the test class execute the above steps:
MyLinkedListStory.java
package com.bdd.jbehave;
import org.jbehave.core.configuration.Configuration;
import org.jbehave.core.configuration.MostUsefulConfiguration;
import org.jbehave.core.junit.JUnitStory;
import org.jbehave.core.steps.InjectableStepsFactory;
import org.jbehave.core.steps.InstanceStepsFactory;
public class MyLinkedListStory extends JUnitStory {
@Override
public Configuration configuration() {
return new MostUsefulConfiguration();
}
@Override
public InjectableStepsFactory stepsFactory() {
return new InstanceStepsFactory(configuration(),
new MyLinkedListSteps());
}
}
Now it's all good to go. Simply run it as JUnit test cases, and you can see the test results:
And there you have it! Now, your stakeholders will have a better idea of what you're trying to do for them. Happy learning!
Opinions expressed by DZone contributors are their own.
Comments