Software Engineering Glossary

JUnit Extension

Also known as: JUnit 5 Extension Jupiter Extension @ExtendWith @RegisterExtension

A JUnit Extension is the Jupiter replacement for a JUnit 4 Rule. You implement callbacks such as BeforeEachCallback or AfterAllCallback, then register the extension with @ExtendWith on a class or with a @RegisterExtension field. Extensions wrap test execution the same way Rules did: shared setup, cleanup, parameter injection, and extra conditions, without copying setUp into every class. Current JUnit 6 docs still use this model on the JUnit Platform.

Key Takeaways

  • New tests on JUnit 5 or 6 should use Extensions, not org.junit.Rule.
  • @ExtendWith is declarative. @RegisterExtension is the instance-field style closest to @Rule.
  • @TempDir, assertThrows, @Timeout, and TestInfo replace the old built-in rules.
  • Spring Boot and Mockito register their own extensions instead of JUnit 4 rules.

How It Works

  1. Jupiter looks for registered extensions and calls their callbacks around each test or class.
  2. ExtensionContext gives you the test class, method, and a store for per-test state.
  3. You can implement several callback interfaces on one class if you need before and after.
  4. JUnit Vintage can still run old Rules on the platform, but it is a migration path, not the long-term API.

Where It Is Used

  • @ExtendWith(SpringExtension.class) or @SpringBootTest wires the Spring test context.
  • @ExtendWith(MockitoExtension.class) initializes @Mock fields.
  • A DatabaseResetExtension that implements BeforeEachCallback is the direct port of a DatabaseResetRule.

Related glossary terms