Junior — Middle
What is the purpose of the @ExtendWith annotation in Java tests and what does it indicate?
sobes.tech AI
Answer from AI
The @ExtendWith annotation in Java tests is used in JUnit 5 to connect extensions that add additional behavior to tests.
It specifies which extension classes should be applied to the test class or method. For example, you can connect an extension for integration with Mockito, managing the test lifecycle, handling parameters, etc.
Example of usage:
@ExtendWith(MockitoExtension.class)
public class MyTest {
@Mock
private MyService service;
@Test
void testService() {
// test using mocks
}
}
Here, MockitoExtension automatically initializes mocks before the tests are run.