JBehave is a flexible Java-based framework for Behavior-Driven Development (BDD) and Test-Driven development (TDD). JBehave is a design philosophy whose purpose is to make TDD and BDD testing more accessible and intuitive. This framework provides a way to create Intelligent Data Types for software quality assurance so that these can be passed as actionable parameters in the code.
For the purpose of this article, let us use a data-type scenario and show how we can set up code for a custom data type. Let’s use an example story that selects the lowest-priced quote from among multiple files in a folder and then places an order.
1 |
Scenario: Select and purchase least pricey quote |
Since the purpose of this article is to discuss intelligent data types in JBehave, we will focus only on how to read the quotations from the folder into a list using our custom data type, that is, just the Given statement.
1 |
@Given("that user has quotations in the folder <quotation>") |
JBehave will look for matching strings after the statements in the story and the code. When it finds an exact match - the Given statements in both - it can map the JAVA code-behind method to the step and execute it. The variable <quotation> should be the same in the story and the code-behind so that the value can be passed from the story file to the JAVA code.
At startup, JBehave calls the overridden configuration() method (Table 3 below).
1 |
@Override private ParameterConverter[] customConverters() { |
In the configuration() method, we set up the array of parameter converters (line 6). The method customConverters() is a helper that returns an array of parameter converters and QuotationsConverter is one of the converters that it returns in the array.
We need to create an object of Quotations data type in which we will store the quotations that are read from the folder. To do this, we need to write a parameter converter - QuotationsConverter.
Why would we use a List within the method customConverter() when it returns an array? Well, a List is easier to manipulate than arrays. Moving up this table, the addConverters() method (line 5) will register this array of parameter converters, including QuotationsConverter, with JBehave.
Next, we declare QuotationsConverter class (Table 4), implementing the methods of interface ParameterConverter - accept() and convertValue().
The accept() method returns true when the type passed is Quotations; it indicates to JBehave that QuotationsConverter wants to process the value. Next, JBehave calls convertValue(), passing it the value it got from the Examples table in the story (Table 1) and the type it needs to be interpreted to, in our case, Quotations.
The convertValue() method will read the quotations folder and parse the quotations in it into a List. This list is then attached to the Quotations object, which will then be returned. The code in Table 4 shows how this method works.
This returned Quotations object will then be passed to the variable quotationMap (Table 2). Now, within Given in the code-behind, we have a Quotations object having data of all quotations; this can be used to weave our logic.
1 |
public class QuotationsConverter implements ParameterConverter { public boolean accept(Type type) { public Object convertValue(String value, Type type) { |
So, to create Intelligent Data Types in JBehave, you must:
1. Declare a parameter class.
2. Declare its corresponding parameter converter and register it with JBehave.
3. Update the code-behind to use this parameter for reading data.
Let’s go through the sequence of steps followed by JBehave :
JBehave is a powerful tool useful for implementing quality testing using BDD and TDD methodologies. At OnPath, our testing expertise includes using a variety of tools like JBehave to ensure full and complete test coverage from a variety of angles.