Scenario writers may want to express parameters in a tabular structure. For example:
Given the traders: |name|rank| |Larry|Stooge 3| |Moe|Stooge 1| |Curly|Stooge 2| When Traders are subset to ".*y" by name Then the traders returned are: |name|rank| |Larry|Stooge 3| |Curly|Stooge 2|
JBehave supports multi-line parameters out-of-the-box and the user only needs to declare the parameter type as ExamplesTable for it to be automatically parsed as a tabular structure:
@Given("the traders: $tradersTable") public void theTraders(ExamplesTable tradersTable) { this.traders = toTraders(tradersTable); } private List toTraders(ExamplesTable table) { List traders = new ArrayList(); List rows = table.getRows(); for (Map row : rows) { String name = row.get("name"); String rank = row.get("rank"); traders.add(new Trader(name, rank)); } Collections.sort(traders); return traders; }
Note: Under the hood, JBehave users the same table parsing functionality of the parametrised scenarios, but there is a fundamental difference between these two use cases: with parametrised scenarios, the scenario is run for each line of the table (using in each execution the parameter values of the given row), while in using tabular parameters we are simply using the tabular structure as a parameter, and how this structure is interpreted is up to the scenario writer. The difference is in the Examples: keyword, which is only present for parametrised scenarios.
By default, value in the table are trimmed, i.e. any preceding and trailing whitespace is removed. This is the most useful and common usecase. If, for some reason, you need to preserve the whitespace, you can specify an optional parsing property:
{trim=false} |name |rank | |Larry|Stooge 3| |Moe |Stooge 1| |Curly|Stooge 2|