Behavior driven development with java and cucumber
Behaviour Driven Development (BDD) is a software development process that originated from the Test driven development (TDD). Requirements, behaviour and results of the software are specified in a domain specific language that can be used by all stakeholders of the project. With this approach nearly everyone should be able to define the behaviour of the software prior to development in a way that is usable for a programmer.
Pitfalls while trying to introduce cucumber and spring in my current project:
Cucumber has a spring implementation, however it is not executing tests with the SpringRunner. Instead it uses a specific CucumberRunner that does not trigger TestExecutionListeners. With this restriction there is for example no trivial solution to trigger flyway migration scripts with integration tests.
Example with cucumber:
Feature definition with a domain specific language called gherkin:
Feature: Programming and Coffe
As a project manager
I want to know how much coffee my programmers consume
Scenario: Programmer with low coffee consumption
Given I employ a Programmer who drinks 200ml of coffe per hour
When He works for 5 hours
Then He drinks 1000ml of coffee
Scenario: Programmer with high coffee consumption
Given I employ a Programmer who drinks 500ml of coffe per hour
When He works for 6 hours
Then He drinks 3000ml of coffee
Implementation
public class MyStepdefs {
Programmer programmer;
int amount;
@Given("I employ a Programmer who drinks {int}ml of coffe per hour")
public void iEmployAProgrammerWhoDrinksMlOfCoffePerHour(int mlOfcoffeePerHour) {
this.programmer = new Programmer(mlOfcoffeePerHour);
}
@When("He works for {int} hours")
public void heWorksForHours(int hoursWorked) {
this.amount = programmer.calculateAmountOfCoffeeConsumed(hoursWorked);
}
@Then("He drinks {int}ml of coffee")
public void heDrinksMlOfCoffee(int amount) {
this.amount = amount;
}
}
Result
There is by the way a possibility to make cucumber work with groovy:
import app.Programmer
import groovy.transform.Field
import static io.cucumber.groovy.EN.*
@Field programmer
@Field result
Given(~/^I employ a Programmer who drinks (\d+)ml of coffe per hour$/) { int mlOfcoffeePerHour ->
this.programmer = new Programmer(mlOfcoffeePerHour)
}
When(~/^He works for (\d+) hours$/) { int hoursWorked ->
result = programmer.calculateAmountOfCoffeeConsumed(hoursWorked)
}
Then(~/^He drinks (\d+)ml of coffee$/) { int amount ->
result == amount
}
Standort Hannover
newcubator GmbH
Bödekerstraße 22
30161 Hannover
Standort Dortmund
newcubator GmbH
Westenhellweg 85-89
44137 Dortmund