JUnit 5 Extension Model

Understand the JUnit 5 extension model and different extensions of JUnit 5.

JUnit 5 prefers extension points over features. Instead of putting all the features in the Jupiter module, JUnit 5 offers an extensible mechanism to better cater to developer needs. Creating temporary files and directories, along with verifying exceptions, are a couple of examples of this.

This philosophy is not new to the JUnit project. In JUnit 4, there is support for Rule and Runner APIs. These allow users to implement their extensions. An example of this extension is MockitoRule. It injects mocks into your unit tests.

In contrast, JUnit 5 consists of a single, coherent extension API. It’s a marker interface. This allows developers to work only with one unified extension API.

JUnit 5 extensions are related to a certain occurrence of an event during the test execution. This kind of event is called an extension point. The Jupiter engine automatically calls the registered extension when the test execution reaches this extension point.

Get hands-on with 1200+ tech skills courses.