Story writers 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:
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 step candidates 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.:
Also note that while the characters delimiting the parameter names in the regex pattern are purely conventional - they only serve the purpose of matching the step method in a readable manner - the use of the angle brackets is required as it is used to replace the name with the value in the reporting.
It is also important to note that the same (@Named
-annotated) methods can match steps
that are executed both as standalone or via examples table, provided that both regex patterns are configured,
one as the main pattern and one as an alias:
Moreover, the examples table alias can happily co-exists with other standalone aliases: