14 JUnit 5 extension model

 

This chapter covers

  • Creating JUnit 5 extensions
  • Implementing JUnit 5 tests using available extension points
  • Developing an application with tests extended by JUnit 5 extensions

The wheel is an extension of the foot, the book is an extension of the eye, clothing an extension of the skin, electric circuitry an extension of the central nervous system.

--Marshall McLuhan

In chapter 4, we demonstrated ways to extend the execution of tests. We examined JUnit 4 rules and JUnit 5 extensions face to face, and we analyzed how to migrate from the old JUnit 4 rules to the new extension model developed by JUnit 5. We also emphasized a few well-known extensions, such as MockitoExtension and SpringExtension. In chapter 8, we implemented tests using mock objects and MockitoExtension, and we’ll implement more tests using SpringExtension in chapter 16. In this chapter, we demonstrate the systematic creation of custom extensions and their applicability to creating JUnit 5 tests.

14.1 Introducing the JUnit 5 extension model

14.2 Creating a JUnit 5 extension

14.3 Writing JUnit 5 tests using the available extension points

14.3.1 Persisting passengers to a database

14.3.2 Checking the uniqueness of passengers

Summary