spring-bootjunitmockitojunit5

Spring Boot JUnit 5 test with @ExtendWith(MockitoExtension.class) not working - mocks are null, MockitoJUnitRunner is working


I just migrated my project from Spring Boot 2.1 to 2.3 and thus have now JUnit 5 (with vintage) in place (also including mockito-core and mockito-junit-jupiter of version 3.3.3). While all JUnit 4 tests are working fine my first JUnit 5 tests is not working correctly:

@ExtendWith(MockitoExtension.class)
public class SomeTest {

    @InjectMocks
    private Some to;

    @Mock
    private SomeProperties properties;

    @Test
    public void applied() {
         ....
         //properties is null -> NPE
         when(properties.getSome()).thenReturn("some");
         ....
    }

The mocks are not injected (NPE in when statement). If i switch to old JUnit 4 style @RunWith(MockitoJUnitRunner.class) all is working fine.

So probably the old runner or vintage runner is used?

How to fix this and get tests with "@ExtendWith" working? I thought i can migrate step by step - let new tests run with junit5 runner.


Solution

  • Ensure you use the correct import for the @Test annotation: