Annotation Interface ExtendWith


@ExtendWith is a repeatable annotation that is used to register extensions for the annotated test class, test interface, test method, parameter, or field.

Annotated parameters are supported in test class constructors, in test methods, and in @BeforeAll, @AfterAll, @BeforeEach, and @AfterEach lifecycle methods.

@ExtendWith fields may be either static or non-static.

Inheritance

@ExtendWith fields are inherited from superclasses as long as they are not hidden or overridden. Furthermore, @ExtendWith fields from superclasses will be registered before @ExtendWith fields in subclasses.

Registration Order

When @ExtendWith is present on a test class, test interface, or test method or on a parameter in a test method or lifecycle method, the corresponding extensions will be registered in the order in which the @ExtendWith annotations are discovered. For example, if a test class is annotated with @ExtendWith(A.class) and then with @ExtendWith(B.class), extension A will be registered before extension B.

By default, if multiple extensions are registered on fields via @ExtendWith, they will be ordered using an algorithm that is deterministic but intentionally nonobvious. This ensures that subsequent runs of a test suite execute extensions in the same order, thereby allowing for repeatable builds. However, there are times when extensions need to be registered in an explicit order. To achieve that, you can annotate @ExtendWith fields with @Order. Any @ExtendWith field not annotated with @Order will be ordered using the default order value. Note that @RegisterExtension fields can also be ordered with @Order, relative to @ExtendWith fields and other @RegisterExtension fields.

Supported Extension APIs

Since:
5.0
See Also:
RegisterExtension, Extension
  • Required Element Summary

    Required Elements
    Modifier and Type
    Required Element
    Description
    Class<? extends Extension>[]
    An array of one or more Extension classes to register.