RESOURCES

Developing Behavior-Driven Tests for JEE Web Applications with Jbehave – Part I

Dec 30, 2019
By: Dani Shemesh

This article is the first of a three-part series about developing behavior-driven tests for JEE web applications with Jbehave.

Behavior-driven development, or BDD, is an agile software development process providing developers, QA, project managers and business teams with a shared tool-set and process for software development collaboration.

In this guide, we’ll learn how to design, develop and automate Black-box tests for a JEE web application using BDD. We’ll do so while developing on top of a Jbehave framework.

Terminology, Tools and the ‘Volcano’ stories

Set out below are the explanations of the main terms used in this blog.

Black-box testing

According to Wikipedia, “Black-box testing is a method of software testing that examines the functionality of an application without peering into its internal structures or workings”. As such, Black-box testing focuses entirely on the inputs and outputs of the software system – the “black box”.

Behavior-driven development (BDD)

Behavior-driven development is an extension of test-driven development that makes use of a simple, domain-specific scripting language.

In BDD, you describe what you want the system to do, by talking through example behavior. Work from the outside-in to implement those behaviors using examples to validate “you are what you are building”.

The customer (perhaps a Scrum Product Owner) describes what they want and the developers ask questions to flesh out enough detail about the behavior to be able to implement it”. (BDD in a nutshell).

Structure:

  • A BDD story is a description of a requirement and its business benefit.  It is also a set of criteria by which we all agree that it is “done”. There should be a story for each feature.
  • A Story consists of a narrative and one or more scenarios.
  • A narrative is a short, simple description of a feature told from the perspective of a person or role that requires the new functionality. The narrative shifts the focus from writing features to discussing them (technologyconversations.com).
  • A scenario consists of steps, in the format of ‘Given-When-Then’. ‘Given’ and ‘When’ trigger actions, and ‘Then’ is the verification:

Frameworks and Tools

Jbehave in a nutshell

JBehave is an open-source framework for behavior-driven development.  It supports Java-based development, and plain English is used to form the story.

The steps in the story are visually linked to a corresponding Java method:

JBehave supports multiple mechanisms for parameter injection. In the above example, the ‘user’ and ‘password’ are arguments extracted from the @When step, with the @named annotation, following a natural order to the parameters in the annotated Java method.

In addition, Jbehave provides an easy way to create more intelligent data types than these strings. There are multiple plugins for generating comprehensive and interactive reports.

There are many (many!) advanced features that are worth checking out (see advanced topics on the Jbehave site); we’ll only be using a few of them.

We’ll explain how Jbehave works at a lower level later.

Thucydides

Thucydides is a tool designed to make writing automated acceptance tests easier.

Thucydides and JBehave work well together. Thucydides uses simple conventions to make it easier to get started with writing and implementing JBehave stories. It reports on both JBehave and Thucydides steps, which can be seamlessly combined in the same class.

Requirements and tools

  • There are JBehave plugins for IntelliJ-Idea and Eclipse. Both come with a custom JBehave Story Editor which provides syntax highlighting, step hyperlink detection and link to a corresponding Java method, step autocompletion, detecting both unimplemented steps and more. Hence, one of these IDEs is required.
  • We use Maven for import libraries, build, run and automated tests
  • You can download and follow the source code through the guide. The dispatcher of the ‘tests’ module is written in Scala. To run it you’ll need a Scala SDK.

Jbehave Plugin and the ‘Volcano’ Stories

We use Idea IntelliJ-IDE with the ‘Jbehave support’ plugin for writing the stories and the code behind it.

Volcano

‘Volcano’ is intended to be a social network for Volcano enthusiasts.

The project manager of ‘Volcano’ would like to add some basic features and behaviors:

  1. User account
    1. Registration to ‘Volcano’.
    2. Change password.
  2. User network
    1. Add a new friend.

Each feature is described in it’s own Jbehave story file.

Here is how the “Registration” story appears before applying the Jbehave support plugin:

 

And after applying the Jbehave support plugin:

At this stage the steps are marked in red, and we receive a message when the mouse hovers over it, that there is no Java method that linked with the steps.

Initial Dependencies

Here Thucydides kicks in.

We’ll use the ‘net.thucydides’ libraries for the implementation.

The following Jbehave plugin artifact:

This includes the Jbehave libraries that are required for the Java implementation code behind it:

 

Jbehave-core provides the basic Jbehave BDD building blocks: The @Given @When @Then Annotations, and the annotations responsible for the parameter injection (i.e. @Named) ‘Jbehave-junit-runner’ provides functionality for the story and scenarios lifecycle and reporting.

Later on, we’ll use it to identify the stories and run the test.

Next

In Part II we’ll implement the ‘Registration’ story, and review solutions to the implementation challenges.

By Dani Shemesh
Read more by this author