Parameter Injection

JBehave supports multiple mechanisms for parameter injection.

Ordered Parameters

This is the default behaviour. The arguments extracted from the step candidate are simply matched following natural order to the parameters in the annotated Java method. For example:

    Given a stock of symbol STK1 and a threshold of 10.0

Arguments "STK1" and "10.0" are matched to the first and second method parameters of the Java method:

    @Given("a stock of symbol $symbol and a threshold of $threshold")
    public void aStock(String symbol, double threshold) {
        // ...
    }

The names of the parameters (symbol and threshold) are not actually relevant, and there is no need for them to match the names of the argument capturers ($symbol and $threshold), although it is good practice to keep them matching.

Annotated Named Parameters

If we want to have named parameters, one mechanism is to use annotations:

    @Given("a stock of symbol $symbol and a threshold of $threshold")
    public void aStock(@Named("symbol") String aSymbol, @Named("threshold") double aThreshold) {
        // ...
    }

One reason to use named parameters is that then we can have method parameter appearing in any order:

    @Given("a stock of symbol $symbol and a threshold of $threshold")
    public void aStock(@Named("threshold") double aThreshold, @Named("symbol") String aSymbol) {
        // ...
    }

Paranamer Named Parameters

An equivalent way to use named parameters, without using annotations, is to leverage Paranamer, configured via the Configuration:

    Paranamer paranamer =  new CachingParanamer(new BytecodeReadingParanamer());
    StepConfiguration configuration = new Configuration();
    configuration.useParanamer(paranamer);
A very common use case of named parameters is for parametrised scenarios.