Search code examples
javabddjbehavethucydidesserenity-bdd

BDD: Embedded tables with serenity and jbehave


I'm trying to create a BDD test with serenity (former thucydides) using the jbehave extension, this is my story (originating from the serenity jbehave examples)

Scenario: a scenario with embedded tables
Given that I sell the following fruit
| fruit  | price |
| apples | 5.00  |
| pears  | 6.00  |
And I sell the following vegetables
| vegetable | price |
| potatoe   | 4.00  |
| carrot    | 5.50  |
When I sell fruit
Then the total cost should be total
Examples:
| goods           | total |
| apples, carrot  | 11.50 |
| apples, pears   | 11.00 |
| potatoe, carrot | 9.50 |

The generated java code is the following:

@Given("that I sell the following fruit\r\n| fruit  | price |\r\n| apples | 5.00  |\r\n| pears  | 6.00  |")
public void givenThatISellTheFollowingFruitFruitPriceApples500Pears600() {
    // PENDING
}

@Given("I sell the following vegetables\r\n| vegetable | price |\r\n| potatoe   | 4.00  |\r\n| carrot    | 5.50  |")
public void givenISellTheFollowingVegetablesVegetablePricePotatoe400Carrot550() {
    // PENDING
}

@When("I sell fruit")
public void whenISellFruit() {
}

@Then("the total cost should be total")
public void thenTheTotalCostShouldBeTotal() {
    // PENDING
}

How do I retrieve the table arguments in my test ?

I tried the ExamplesTable parameters as per documentation on jbehave tabular parameters but that did not work.

Is there a way to make the given annotation more readable (by not adding the table parameters) ?


Solution

  • You can retrieve the ExampleTable parameter like this (and have more readable given annotations):

    @Given("that I sell the following fruit $exampleTable")
    public void thatISellTheFollowingFruit(ExamplesTable exampleTable) {
        System.out.println("MyTable: "+exampleTable.asString());
    }
    

    If it doesn't find the declared method and tells you that this step is pending, you could check if you have a whitespace in your story after the word fruit:

    Given that I sell the following fruit 
    

    How to access the several rows and columns in your tables is written in the jBehave documentation under http://jbehave.org/reference/stable/tabular-parameters.html

    You could also think about creating only one table instead of three:

    Scenario: a scenario with embedded tables
    Given I sell <product1> 
    And the price is <product1price>
    And I sell <product2> 
    And the price is <product2price>
    When I sell something
    Then the total cost should be <total>
    Examples:
    | product1 | product1price | product2 | product2price | total |
    | apples   | 5.00          | carrot   | 6.50          | 11.50 |
    | apples   | 5.00          | pears    | 6.00          | 11.00 |
    | potatoe  | 4.00          | carrot   | 9.50          | 13.50
    

    The java code would have to look like this to access the parameters:

    @Given("I sell <product1>")
    public void iSellProduct(@Named("product1") String product1) {
        //do something with product1
    }
    

    Does this help? If not, what exactly does not work when you try to read the exampleTable?