@ExtendWith JUnit 5 | Java Fresher Interview
Fast recall
Quick revision
Core answer
Interview-ready guide
Interview answer
ExtendWith is how I register an extension in JUnit 5, and an extension is basically a plugin that hooks into the test lifecycle. It can run code before and after tests, resolve parameters, or post-process the test instance. I just point ExtendWith at the extension class and JUnit wires it in.
The two I use all the time are the Mockito extension and the Spring extension. The Mockito extension initializes my Mock and InjectMocks fields and also reports unnecessary stubbing, which catches mistakes. The Spring extension boots up the Spring application context so I can autowire beans in a test.
The history here matters. In JUnit 4 you used RunWith, and the big limitation was that you could only have one runner per class. So you had to choose between, say, the Mockito runner and the Spring runner. JUnit 5 replaced that with the extension model, and the key improvement is that it's composable — I can stack multiple extensions on the same class. That's a real practical win.
The most common fresher mistake is still using RunWith with JUnit 5, in which case it's just ignored and your Mock fields come back null, so you get a NullPointerException on the first stub. The fix is ExtendWith with the Mockito extension. Another subtle point is that you don't add the Spring extension manually when you're already using SpringBootTest or WebMvcTest, because those meta-annotations already include it.
@ExtendWith JUnit 5 | Java Fresher Interview
Direct Answer
@ExtendWith registers a JUnit Jupiter extension. An extension can provide lifecycle callbacks, parameter resolution, exception handling, or test-instance processing. MockitoExtension initializes Mockito annotations and applies Mockito's strictness rules; SpringExtension connects Jupiter to Spring's TestContext framework, whose other annotations decide which context is loaded. Multiple extensions can be registered, unlike JUnit 4's one-runner model.
Deep Dive & Architecture
@ExtendWith registers one or more JUnit Jupiter extensions. An extension can take part in discovery, create or resolve parameters, run setup and cleanup callbacks, or react to test results. This is the JUnit 5 replacement for many jobs previously handled by JUnit 4 runners and rules.
For example, @ExtendWith(MockitoExtension.class) initializes @Mock fields and resolves Mockito’s test lifecycle. Spring’s testing support uses SpringExtension to connect the TestContext framework. Multiple extensions can be declared because the extension model is composable; a JUnit 4 class could have only one @RunWith runner. Register only extensions the test needs, because an extension can add setup cost or alter lifecycle behavior.
Common Mistakes & Pitfalls
- Mixing JUnit 4 and Jupiter models.
@RunWithbelongs to JUnit 4; Jupiter uses@ExtendWith. A JUnit 4 test can still run through the Vintage engine, so check imports and engine configuration rather than assuming it is ignored. - Forgetting Mockito initialization.
@Mockfields needMockitoExtensionor a correctly managedopenMockscall. - Re-adding SpringExtension.
@SpringBootTest,@WebMvcTest, and@DataJpaTestalready include it. - Mixing Mockito initialization styles. Choose the extension or
MockitoAnnotations.openMocks, and close the latter's returned handle.
Check your understanding
Answer these without looking back at the article.
- What is the purpose of @BeforeEach and @AfterEach in JUnit 5?
- What does @DisplayName do in JUnit 5 and why should you use it?
- What is @Nested in JUnit 5 and how does it improve test organization?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.