Behavior-driven development (BDD)
Behavior-driven development is an Agile software development process that supports collaboration among the developers, quality analysts, and business members in a software project. It is developed from the Test-driven development (TDD) software.
The BDD is a combination of general techniques and principles of the TDD with the ideas originated from the Domain-driven design (DDD) and the object-oriented analysis and design (OOAD) approach.
Mockito uses the BDDMockito class that is available in the org.mockito package. It develops a test in BDD style. The BDD style of writing texts uses the //given //when //then comments as the primary part of the test body. It uses given(..)willReturn(..) method in place of when(..)thenReturn(..) method.
Following are some important methods of the BDDMockito class:
The following code snippet shows how to use the BDD style test:
In the above code snippet, we have used assertThat() method instead of assertEquals() method. It makes the code more readable and understandable if they are referenced through static import.
Example of BDD style
Here, we are going to create an example of a BDD style test. Switching to BDD style makes a minor difference only in the test syntax. It splits the test syntax into three parts: given, when, and then that makes the code more readable.
Let's understand the BBD style test by creating an example.
The following output shows that the test is successfully running using the BDD style.