21 Behavior-driven development with JUnit 5

 

This chapter covers

  • Analyzing the benefits and challenges of BDD
  • Moving a TDD application to BDD
  • Developing a BDD application with Cucumber and JUnit 5
  • Developing a BDD application with JBehave and JUnit 5

Some people refer to BDD as “TDD done right.” You can also think of BDD as “how we build the right thing” and TDD as “how we build the thing right.”

--Millard Ellingsworth

As we discussed in chapter 20, test-driven development (TDD) is an effective technique that uses unit tests to verify the code. Despite TDD’s clear benefits, its usual loop

[test, code, refactor, (repeat)]

can cause developers to lose the overall picture of the application’s business goals. The project may become larger and more complex, the number of unit tests will increase, and those tests will become harder to understand and maintain. The tests may also be strongly coupled with the implementation. They focus on the unit (the class or the method) that is tested, and the business goals may not be considered.

Starting from TDD, a new technique has been created: behavior-driven development (BDD). It focuses on the features themselves and makes sure they work as expected.

21.1 Introducing behavior-driven development

21.1.1 Introducing a new feature

21.1.2 From requirements analysis to acceptance criteria

21.1.3 BDD benefits and challenges

21.2 Working BDD style with Cucumber and JUnit 5

21.2.1 Introducing Cucumber

21.2.2 Moving a TDD feature to Cucumber

21.2.3 Adding a new feature with the help of Cucumber

21.3 Working BDD style with JBehave and JUnit 5

21.3.1 Introducing JBehave

21.3.2 Moving a TDD feature to JBehave