Extension Context

Understand how to build ExtensionContext in JUnit 5.

The ExtensionContext interface

When building extensions, the most important concept to understand is the ExtensionContext interface. It contains context information about the currently running test class or method. Instances of ExtensionContext pass to extensions as parameters that allow them to access necessary context information. They’re organized in a hierarchical way. An ExtensionContext can have a parent ExtensionContext. For example, the ExtensionContext of a test class is the parent of the ExtensionContext of a test method in this class. The ExtensionContext of a nested class annotated with @Nested is the child of the ExtensionContext of its parent class.

The table below shows the methods of ExtensionContext.

Get hands-on with 1200+ tech skills courses.