Behavior Driven Development with JUnit 5. Part 5

Behavior Driven Development with JUnit 5. Part 5

The fifth part of our article on Behavior Driven Development with JUnit 5. Happy reading.
We’ll now create a new Java class in the test/java folder, in the com.luxoft.bddjunit5.airport package. This class will be named PassengerPolicy and, at first, will contain the test skeleton. The execution of such a test follows the scenarios described in the passenger_policy.feature file. For example, when executing the step

Given there is an economy flight

the program will execute the method annotated with

@Given("^there is an economy flight$")

public class PassengerPolicy {

   @Given("^there is an economy flight$")                                #1

   public void there_is_an_economy_flight() throws Throwable {           #2

    // Write code here that turns the phrase above into concrete actions #2

    throw new PendingException();                                        #2

   }                                                                     #2

 

   @When("^we have a regular passenger$")                                #3

   public void we_have_a_regular_passenger() throws Throwable {          #4

   // Write code here that turns the phrase above into concrete actions  #4

      throw new PendingException();                                      #4

   }                                                                     #4

 

   @Then("^you can add and remove him from an economy flight$")          #5

   public void you_can_add_and_remove_him_from_an_economy_flight()       #6

      throws Throwable {                                                 #6

   // Write code here that turns the phrase above into concrete actions  #6

      throw new PendingException();                                      #6

   }                                                                     #6

 

   [...]

 

}


In this listing:

  • The Cucumber plugin generates a method annotated with @Given("^there is an economy flight$"), meaning this method is executed when the step Given there is an economy flight from the scenario is executed #1.
  • The plugin generates a method stub to be implemented with the code addressing the step Given there is an economy flight from the scenario #2.
  • The plugin generates a method annotated with @When("^we have a regular passenger$"), meaning this method is executed when the step When we have a regular passenger from the scenario is executed #3.
  • The plugin generates a method stub to be implemented with the code addressing the step When we have a regular passenger from the scenario #4.
  • The plugin generates a method annotated with @Then("^you can add and remove him from an economy flight$"), meaning this method is executed when the step. Then you can add and remove him from an economy flight from the scenario is executed #5.
  • The plugin generates a method stub to be implemented with the code addressing the step Then you can add and remove him from an economy flight from the scenario #6.
  • The rest of the methods are implemented in a similar way; we have covered the Given, When, and Then steps of one scenario.

Behavior Driven Development with JUnit 5_Part 5.jpg


We follow the business logic of each step that has been defined and translates it into the tests—the steps of the scenarios that need to be verified.

public class PassengerPolicy {

    private Flight economyFlight;                                        #1

    private Passenger mike;                                              #1

    [...]

 

    @Given("^there is an economy flight$")                               #2

    public void there_is_an_economy_flight() throws Throwable {          #2

        economyFlight = new EconomyFlight("1");                          #3

    }

 

    @When("^we have a regular passenger$")                               #4

    public void we_have_a_regular_passenger() throws Throwable {         #4

        mike  = new Passenger("Mike", false);                            #5

    }

 

    @Then("^you can add and remove him from an economy flight$")         #6

    public void you_can_add_and_remove_him_from_an_economy_flight()      #6

           throws Throwable {

        assertAll("Verify all conditions for a regular passenger         #7

                   and an economy flight",                               #7

          () -> assertEquals("1", economyFlight.getId()),                #7

          () -> assertEquals(true, economyFlight.addPassenger(mike)),    #7

          () -> assertEquals(1,                                          #7

                  economyFlight.getPassengersSet().size()),              #7

          () ->

          assertTrue(economyFlight.getPassengersSet().contains(mike)),   #7

          () -> assertEquals(true, economyFlight.removePassenger(mike)), #7

          () -> assertEquals(0, economyFlight.getPassengersSet().size()) #7

        );

    }

    [...]

 }


In this listing:

  • We declare the instance variables for the test, including economyFlight and mike as a Passenger #1.
  • We write the method corresponding to the Given there is an economy flight business logic step #2 by initializing economyFlight #3.
  • We write the method corresponding to the When we have a regular passenger business logic step #4 by initializing the regular passenger mike #5.
  • We write the method corresponding to the Then you can add and remove him from an economy flight business logic step #6 by checking all the conditions using the assertAll JUnit 5 method, which can now be read fluently #7.
  • The rest of the methods are implemented in a similar way; we have covered the Given, When, and Then steps of one scenario.

Interested in JUnit? Check out our trainings.

Catalin Tudose
Java and Web Technologies Expert


Mai ai întrebări?
Conectați-văcu noi