Configuring deterministic execution order for multiple @ExtendWith annotations in JUnit 5
0 reputation · 24 Jul 2025, 20:03 UTC
Goal: Ensure predictable execution order of multiple JUnit 5 extensions
When a test class is annotated with several @ExtendWith entries, each registering extensions that implement lifecycle callbacks such as BeforeEachCallback or AfterEachCallback, the JUnit Platform does not guarantee a deterministic invocation order unless the extensions also implement Orderable or are declared via @RegisterExtension with an explicit sequence. This unspecified order is noted as an implementation detail that may change between JVMs, classloader layouts, or JUnit releases, which can produce intermittent failures that are hard to trace from deployment logs.
The goal is to achieve a stable, repeatable order for these extensions without requiring each extension to implement Orderable or consolidating logic into a single extension, and without relying on the current annotation declaration order, which is not documented as a guarantee.
Is there a mechanism to declare a global ordering rule for @ExtendWith annotations? Can the JUnit Platform be configured to treat annotation declaration order as a stable default? Does setting a specific junit-platform.properties flag enforce deterministic extension invocation?