Site icon TestingDocs.com

Annotated @Named Parameters Example

Introduction

In this post, we will discuss Annotated @Named Parameters. We will see how to pass arguments values to java method parameters from story gherkin textual steps. For example, the below gherkin and its matching java method is as shown below:

Textual step:

Given numbers to add 7 and 9

Annotated java method:

@Given("numbers to add $i and $j")

public void givenNumbers(@Named("i")int i,@Named("j")int  j )

{...}

 

@Named Parameters


 

Multiple Scenarios

Sample story with multiple scenarios reusing the same annotated java method:

Narrative:
Checking simple ADD functions in this story

Scenario: ADD single digit numbers 
 
Given numbers to add 7 and 9
When we add them
Then verify the sum


Scenario: ADD double digit numbers 
 
Given numbers to add 10 and 15
When we add them
Then verify the sum


Scenario: ADD triple digit numbers 
 
Given numbers to add 100 and 250
When we add them
Then verify the sum


The default JBehave behavior is that the arguments extracted from the gherkin are matched
with the order to the parameters in the annotated Java method.
The names of the parameters are not actually relevant, and there is no need for them to match the names of the substituted argument with $. It’s good practice to keep them same for readability and easy story debugging sake.

 

JBehave Tutorials on this website can be found at:
https://www.testingdocs.com/jbehave-framework-tutorial/

For more details on the JBehave framework, visit the official JBehave website at:
http://jbehave.org

Exit mobile version