Find a tool to support BDD in Java environment

Behavior-driven development is a specialized version of test-driven development which focuses on behavioral specification of software units. Acceptance tests are written using the standard agile framework of a User story: "As a [role] I want [feature] so that [benefit]". Acceptance criteria should be written in terms of scenarios and implemented as classes: Given [initial context], when [event occurs], then [ensure some outcomes]. The goal is to find a tool for the Java environment to support the writing of scenarios, support for creating the java classes and methods for a scenario step.

Taggings:

1 answer

JBehave plugin for eclipse

jBehave plugin brings the following features:
- Step auto-completion
- Syntax highlighting
- Step hyperlink detection and link to corresponding Java step method
- Step validation, detecting both unimplemented steps and ambiguous steps

How to install and use the jBehave plugin:
- In Eclipse go to Help > Install New Software
- Add the new site location http://jbehave.org/reference/eclipse/updates/
- Select JBehave Eclipse feature and click next
- Take defaults for the rest of the prompts
- Restart Eclipse

Create a jBehave story
- right click on the package where you want to place you feature file
- select New->Other and the jBehave->New Story wizard
- in the Story dialog, name the story file name
- click finish

Example file:
Sample story

Narrative:
In order to communicate effectively to the business some functionality
As a development team
I want to use Behaviour-Driven Development

Scenario: A scenario is a collection of executable steps of different type
Given step represents a precondition to an event
When step represents the occurrence of the event
Then step represents the outcome of the event

Create a class with the step automation from a scenario
- right click on the package you want to put the step automation
- select New->Class
- in the New Java Class enter the class name you have choosen
- click finish

For the example scenario file above we could genearte the following step implementations

package com.joe.steps;

import org.jbehave.core.annotations.Given;
import org.jbehave.core.annotations.Then;
import org.jbehave.core.annotations.When;
import org.jbehave.core.steps.Steps;

public class ExampleSteps extends Steps{

@Given("step represents a precondition to an event")
public void givenStepRepresentsTheOccurrenceOfTheEvent(){
//ToDo implementation
}

@When("step represents the occurrence of the event")
public void whenStepRepresentsTheOccurrenceOfTheEvent(){
//ToDo implementation
}

@Then("step represents the outcome of the event")
public void thenStepRepresentsTheOccurrenceOfTheEvent(){
//ToDo implementation
}
}

A simple way for running the tests is:
private static Embedder embedder = new Embedder();
private static List storyPaths = Arrays
.asList("[[Package]]/[StoryName].story");

public static void main(String[] args) {
embedder.candidateSteps().add(new ExampleSteps());
embedder.runStoriesAsPaths(storyPaths);
}

Taggings: