BDD using JGiven – How It Can Make Your Work Easier?

BDD using JGiven – How It Can Make Your Work Easier?

BDD or Behavior-Driven-Development is a method of programming in which business clients, developers and testers describe the behavior of the application with the help of scenarios. The scenarios are written using agreed notation and language, so that they are understood by all parties.

JGiven library at BBD’s service

Today I will present to you JGiven library, which aids precisely this process. Solutions for Java, such as JBehave or Cucumber, are already in use, so what can JGiven library offer to us? You should know that:

  • scenarios are written in Java, rather than, as in the case of other libraries, using plain text,
  • scenarios are are executed by JUnit or TestNG,
  • scenarios are divided into stages, and state of their implementation is passed between them via injections,
  • JGiven generates reports of scenario completion.

Usage example

This is a test which might have been prepared by a customer.

public void we_can_buy_a_ticket_from_wroclaw_to_berlin_for_ten_euros(){



You can see the division into 3 stages: Given, When and Then. This allows for separation of the logic of the test to individual classes.

Test class extends the class ScenarioTest and passes class types for each stage.

public class TicketsMachineScenarioTest extends ScenarioTest<GivenCities, WhenBuy, ThenTicket> 

Each stage is a class expanding Stage. We pass data between stages using annotations @SenarioState, @ProvidedScenarioState, @ExpectedScenarioState. Each of these annotations works the same way, their different names are created only in order to better reproduce the context of use.

These are the end stages for this test.


public class GivenCities extends Stage<GivenCities> {

    String originCity;
    String destinationCity;

    public GivenCities an_origin_city(String city) {
        originCity = city;
        return this;

    public GivenCities destination_city(String city) {
        destinationCity = city;
        return this;

public class WhenBuy extends Stage<WhenBuy> {

    TicketsMachine ticketsMachine;

    String originCity;
    String destinationCity;

    Ticket ticket;

    public WhenBuy(){
        ticketsMachine = new TicketsMachine();

    public WhenBuy we_press_button() {
        assertNotNull( ticketsMachine );
        ticketsMachine.selectCities(originCity, destinationCity);

        return this;

    public WhenBuy insert_money(float money) {
        assertNotNull( ticketsMachine );
        ticket = ticketsMachine.buyTicket(money);

        return this;

public class ThenTicket extends Stage<ThenTicket> {

    Ticket ticket;

    public void the_resulting_ticket_should_be_valid_for_travel_from_wroclaw_to_berlin() {

Methods in Given and When stages should best return reference to their own data, so that we can use basic methods of the Stage class and combine our calls using and(), with(), but().

Final report

The test report generated for this example looks like this:

We can buy a ticket from wroclaw to berlin for ten euros

Given an origin city Wroclaw

And destination city Berlin

When we press button

And insert money 10.0

Then the resulting ticket should be valid for travel from wroclaw to berlin.


JGiven library is a very interesting tool aiding software development process. It is quite simple to use, yet has a lot of mechanisms making work easier. This allows for easy separation of test phases and creating test scenarios in Java, which reduces the amount of work a programmer has to do. Based on my experience with it, I encourage you to check it. More information can be found on page


Grzesiek Lipski

Grzesiek has been specializing in professional Android app development for 9 years. He likes modern solutions and always pays attention to high-quality code. Grzesiek eagerly tests new possibilities, so he also builds apps with Flutter, and after hours he tries his hand at game development in Unity3D. When he’s not programming, he plays squash or takes photos with a drone.

Learn more

ConstraintLayout, or not so fast, after all…

Recently, while browsing through news from Android world I came across the concept of ConstraintLayout. It is a new layout delivered by Android and Google, supporting Android versions from API 9 on. Digging into possibilities it is to give, I decided to check how the new Layout Builder behaves and what is ConstraintLayout like in use.

Read more

Android Small Talks: CoordinatorLayout, or one of the strengths of Android Design Support Library

When Android Lollipop entered the market, there was a breakthrough. Google provided us with an extensive library of Android Design Support Library which facilitates creating applications that are compliant with the principles of Material Design. Creating a user interface in accordance with these guidelines introduces our software to the next level of design and user-application interaction.

Read more

Project estimation

Let us know what product you want to build and how we can help you.

Why choose us?

Logo Mobile Trends Awards

Mobile Trends Awards 2021

Winning app in

Nagroda Legalnych Bukmacherów

Legal Bookmakers Award 2019

Best Mobile App

Mobile Trends Awards logo

Mobile Trends Awards 2023



client reviews

Clutch logo