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/