Docs

Documentation versions (currently viewingVaadin 14)

You are viewing documentation for an older Vaadin version. View latest documentation

Behavior-Driven Development

Behavior-driven development (BDD) is a development methodology based on test-driven development, where development starts from writing tests for the software-to-be. BDD involves using a ubiquitous language to communicate between business goals - the desired Behavior - and tests to ensure that the software fulfills those goals.

The BDD process starts by collection of business requirements expressed as user stories, as is typical in agile methodologies. A user with a role requests a feature to gain a benefit.

Stories can be expressed as number of scenarios that describe different cases of the desired Behavior. Such a scenario can be formalized with the following three phases:

  • Given that I have calculator open

  • When I push calculator buttons

  • Then the display should show the result

This kind of formalization is realized in the JBehave BDD framework for Java. The TestBench Demo includes a JBehave example, where the above scenario is written as the following test class:

public class CalculatorSteps extends TestBenchTestCase {
    private WebDriver driver;
    private CalculatorPageObject calculator;

    @BeforeScenario
    public void setUpWebDriver() {
        driver = TestBench.createDriver(new FirefoxDriver());
        calculator = PageFactory.initElements(driver,
                CalculatorPageObject.class);
    }

    @AfterScenario
    public void tearDownWebDriver() {
        driver.quit();
    }

    @Given("I have the calculator open")
    public void theCalculatorIsOpen() {
        calculator.open();
    }

    @When("I push $buttons")
    public void enter(String buttons) {
        calculator.enter(buttons);
    }

    @Then("the display should show $result")
    public void displayShows(String result) {
        assertEquals(result, calculator.getResult());
    }
}

The demo employs the page object defined for the application UI, as described in Creating Maintainble Tests using Page Objects.

Such scenarios are included in one or more stories, which need to be configured in a class extending JUnitStory or JUnitStories. In the example, this is done in the https://github.com/vaadin/testbench-demo/blob/master/src/test/java/com/vaadin/testbenchexample/bdd/SimpleCalculation.java class. It defines how story classes can be found dynamically by the class loader and how stories are reported.

For further documentation, please see JBehave website at jbehave.org.

41AD4593-2F0A-48D6-ADF0-89C7007D6357