Scenario writer often find themselves repeating scenarios, or parts thereof, by simply changing some parameter values. These are ideal candidates for using JBehave Table Examples feature. Let's look at the example:
Given a stock of symbol STK1 and a threshold of 10.0 When the stock is traded at 5.0 Then the alert status should be OFF When the stock is traded at 11.0 Then the alert status should be ON
We notice that two lines are repeated and identical but for the values. We can then rewrite this scenario as:
Given a stock of symbol [symbol] and a threshold of [threshold] When the stock is traded at [price] Then the alert status should be [status] Examples: |symbol|threshold|price|status| |STK1|10.0|5.0|OFF| |STK1|10.0|11.0|ON|
The Examples: keyword signals that the scenario should be repeated for as many times as there are data rows in the examples table. At each execution, the named parameters are taken from the corresponding row.
One important difference to underline in using table examples is that they require named parameters for the candidate steps to be matched to Java methods. The named parameters allow the parameters to be injected using the table row values with the corresponding header name, instead of being extracted from the annotation pattern match. As such, the step annotation pattern must hold the verbatim textual step. e.g.:
@Given("a stock of symbol [symbol] and a threshold of [threshold]") public void aStock(@Named("symbol") String symbol, @Named("threshold") double threshold) { // ... }