Behavior Driven Development cu JUnit 5. Partea a cincea

Inca un articol din seria noastra despre Behavior Driven Development cu JUnit 5. Lectura placuta.

Vom crea acum o noua clasa Java in fisierul test/java, din pachetul com.luxoft.bddjunit5.airport. Aceasta clasa se va numi PassengerPolicy si, la inceput, va contine test skeleton. Executia unui asemenea test urmareste scenariile descrise in fisierul passenger_policy.feature. Spre exemplu, cand executam pasul

Se da un zbor la clasa economy

programul va executa metoda adnotata cu

@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 codul de mai sus:

  • Pluginul Cucumber genereaza o metoda adnotata cu @Given(“^there is an economy flight$”), insemnand ca aceasta metoda este executata atunci cand pasul Given there is an economy flight din scenariu este executat #1.
  • Pluginul genereaza o method stub care sa fie implementata cu codul adresand astfel pasul Given there is an economy flight din scenariul #2.
  • Pluginul genereaza o metoda adnotata cu @When(“^we have a regular passenger$”), insemnand ca aceast metoda este executata cand este executat pasul When we have a regular passenger din cadrul scenariului #3.
  • Pluginul genereaza o method stub care sa fie implementata cu codul adresand pasul When we have a regular passenger din scenariu #4.
  • Pluginul genereaza o metoda adnotata cu @Then(“^you can add and remove him from an economy flight$”), insemnand ca aceasta metoda este executata atunci cand este executat pasul Then you can add and remove him from an economy flight din scenariu #5.
  • Pluginul genereaza o method stub care sa fie implementata cu codul adresand pasul Then you can add and remove him from an economy flight din scenariul #6.
  • Restul metodelor sunt implementate intr-un mod similar; am abordat doar pasii Given, When, si Then dintr-un scenariu

Urmam business logic a fiecarui pas care a fost definit si il traducem in teste — pasii din scenariu care trebuie verificati.

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 codul de mai sus:

  • Declaram instance variables pentru test, inclusiv economyFlight si mike ca Passenger #1.
  • Scriem metoda care corespunde pasului din business logic pentru Given there is an economy flight #2 initializand economyFlight #3.
  • Scriem metoda care corespunde pasului din business logic pentru When we have a regular passenger #4 initializand pasagerul normal mike #5.
  • Scriem metoda care corespunde pasului din business logic pentru Then you can add and remove him from an economy flight #6 verificand toate conditiile folosind metoda JUnit 5 assertAll, care acum poate sa fie citita fluent #7.
  • Restul metodelor sunt implementate in mod similar; am abordat pasii Given, When, si Then dintr-un scenariu.

Vrei sa inveti mai multe despre aceasta tehnologie? Descopera cursurile noastre.

Catalin Tudose
Java and Web Technologies Expert

Originally published at https://www.luxoft-training.ro.