Aliases

Behaviour-Driven Development emphasises the importance of language in communication the behaviour of the system from a business perspective. As such, the language used in the textual stories and scenarios is subject to change, evolving with the understanding or the definition of the system itself.

It is quite common therefore to want to express the same underlying step functionality in slightly different ways, or to support multiple syntaxes.

JBehave addresses this use cases with aliases. For example, let's consider the following step:

    @When("the item price is $price")
    public void theItemPriceIs(double price) {
        // ...
    }

which matches the following textual step:

When the item price is 10.0

At some point in the evolution of the description of the behaviour of the system, it may become necessary to underline the evolving nature of the item price, i.e. that the price changes and assumes a different value. In this case, it may be a more appropriate description to use the verb becomes in place of is:

When the item price becomes 10.0

We can then add an Alias annotation with the new matching step pattern:

    @When("the item price is $price")
    @Alias("the item price becomes $price") // single alias
    public void theItemPriceIs(double price) {
        // ...
    }

JBehave will then create for this method two candidate steps, both of type WHEN, one for each of the two regex patterns, "the item price is $price" and "the item price becomes $price".

An obvious generalisation of this concept is the support for multiple aliases, via the Aliases annotation:

    @When("the item price is $price")
    @Aliases(values={"the item price becomes $price"
                     "the item price equals to $price"}) // multiple aliases 
    public void theItemPriceIs(double price) {
        // ...
    }

Along with code approach with Alias and Aliases it's also possible to load aliases from external resources by specifing resource paths using useAliasPaths(Set) method and alias parser using useAliasParser(AliasParser) method. By default the JsonAliasParser is used.

    [
        {
            "name": "When the item price is $price",
            "aliases":
            [
                {
                    "name": "When the item price becomes $price"
                },
                {
                    "name": "When the item price equals to $price"
                }
            ]
        }
    ]
If the variations defined in the aliases are small or localised in word alternatives, it may be simpler and more compact to use pattern variants.