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.

@Test
public void 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.0f);

    then().the_resulting_ticket_should_be_valid_for_travel_from_wroclaw_to_berlin();
}

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.

Given 

public class GivenCities extends Stage<GivenCities> {

    @ProvidedScenarioState
    String originCity;
    @ProvidedScenarioState
    String destinationCity;

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

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

public class WhenBuy extends Stage<WhenBuy> {

    TicketsMachine ticketsMachine;

    @ExpectedScenarioState
    String originCity;
    @ExpectedScenarioState
    String destinationCity;

    @ProvidedScenarioState
    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;
    }
}
Then

public class ThenTicket extends Stage<ThenTicket> {

    @ExpectedScenarioState
    Ticket ticket;

    public void the_resulting_ticket_should_be_valid_for_travel_from_wroclaw_to_berlin() {
        assertNotNull(ticket);
        assertTrue(ticket.isOriginCity("Wroclaw"));
        assertTrue(ticket.isDestinationCity("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 http://jgiven.org/

Grzesiek

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

5 Reasons Why You Should Build the UI in ConstraintLayout

ConstraintLayout is a popular solution that Android developers use every day. And not without a good reason. If you’re at the beginning of your journey as a software engineer, get familiar with the most important features this layout can offer. It will help you understand the way it works, and Android app development will become easier.

Read more

Bitrise Tests Made Easier: Update JIRA Issues with Build Number and Forget About Delays in QA Testing

When you trigger Bitrise build with changes and forget to tell QA specialists some essential information, there are two likely scenarios. You waste time waiting for the update from the tester, only you don’t know it’s never coming. Or you get so many questions about builds that you can’t keep up. Sounds familiar? If so, let us tell you about the JIRA issue update that keeps workflow in order.

Read more

How to Implement Feature Flags and Gain More Control over Your App?

Every person with software development experience can share a few stories about incorrectly working features. We build our apps out of small pieces, introduce advanced architecture patterns, and yet sometimes one of the elements fails, causing bugs or even system failures. In such cases, feature toggling can save the day! Find out how to implement feature flags, and make your app more stable.

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 2017

Nomination in
M-COMMERCE category

Logo Legalni bukmacherzy

Legal Bookmakers Award 2019

Best Mobile App

Mobile Trends Awards logo

Mobile Trends Awards 2020

Nomination in SPORTS & RECREATION category

18

client reviews

Clutch logo