Outcomes Table

A typical use case when outcomes are collected in a tabular format is to verify them all at once. To this end, JBehave provides the OutcomesTable:

@Then("the values returned are: $table")
public void theValuesReturnedAre(ExamplesTable table) {
    Map<String,String> actual = actualValues(); // obtained from another step invocation
    OutcomesTable outcomes = new OutcomesTable();
    Map<String,String> expected = table.getRow(0); // assuming all values are contained in a single row
    for ( String key : expected.keySet() ){
        outcomes.addOutcome(key, actual.get(key), Matchers.equalTo(expected.get(key)));           
    }
    outcomes.verify();
}

The user adds each outcome using a Hamcrest Matcher:

public <T> void addOutcome(String description, T value, Matcher<T> matcher);
The Matcher interface is contained in the hamcrest-core module. A collection of Matchers is contained in the hamcrest-library. You may choose to upgrade just the library module or provide your own custom implementations of Matcher.

Upon verification, the outcome matching is done and if any outcome verification fails an exception is thrown and the step execution fails and the entire outcomes table (including all verified outcomes) is reported to the StoryReporter. The TXT format would look like:

|Description|Value|Matcher|Verified|
|Value1|Value1|"Value1"|Yes|
|Value2|Value2|"ValueX"|No|

The HTML format would use colour-coding for failures.

Using different Locales

The OutcomesTable is localisable, via the injection of the appropriate LocalizedKeywords.

new OutcomesTable(new LocalizedKeywords(new Locale(...)));

The TXT format would look like, in FR locale:

|Déscription|Valeur|Matcher|Vérifié|
|Value1|Value1|"Value1"|Oui|
|Value2|Value2|"ValueX"|Non|