Table Parameters

Scenario writers may want to express a parameter 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|

Now, JBehave supports multi-line parameters out-of-the-box. Next, we need only combine this feature with the table parsing functionality already seen in the table examples:

    @Given("the traders: $tradersTable")
    public void theTraders(ExamplesTable tradersTable) {
        // tradersTable is automatically parsed using the multi-line String input
        // next we need to interpret its content to create a list of traders
        // then we can use the traders create in subsequent steps
        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: We are using the same table parsing functionality of the table examples, via the re-use of the ExamplesTable, but there is a fundamental difference between these two use cases: with table examples the scenario is run for each line of the table (using in each execution the parameter values of the given row), while in using table parameters we are simply using the tabular structure as a parameter, and how this structure is interpreted is up to the scenario writer.