Vaadin Testbench - Best Practices - Behaviour-Driven Development
 Edit This Page

Behaviour-Driven Development

Behaviour-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 behaviour - 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 behaviour. 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 "The Page Object Pattern".

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.