Mockito does not initialize a mock run with the JUnit 5 in a @BeforeAll
annotated method.
It works if I change the init
's method annotation to @BeforeEach
. Tests are run within IntelliJ IDEA.
My test class :
@ExtendWith(MockitoExtension.class)
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
public class MyTest {
private MyMockedClass myMockedClass;
@BeforeAll
public void init() {
when(myMockedClass.getSomething()).thenReturn(something); // Mock is not initialized, getting NPE on test
Dependencies (only related ones are shown, others omitted for brevity):
[INFO] --- maven-dependency-plugin:2.10:tree (default-cli) @ XXX ---
[INFO] +- org.mockito:mockito-core:jar:3.6.28:test
[INFO] | +- net.bytebuddy:byte-buddy:jar:1.10.18:compile
[INFO] | +- net.bytebuddy:byte-buddy-agent:jar:1.10.18:test
[INFO] | \- org.objenesis:objenesis:jar:3.1:test
[INFO] +- org.mockito:mockito-junit-jupiter:jar:3.6.28:test
[INFO] | \- org.junit.jupiter:junit-jupiter-api:jar:5.7.0:test
[INFO] | +- org.apiguardian:apiguardian-api:jar:1.1.0:test
[INFO] | +- org.opentest4j:opentest4j:jar:1.2.0:test
[INFO] | \- org.junit.platform:junit-platform-commons:jar:1.7.0:test
[INFO] +- org.springframework.boot:spring-boot-starter-test:jar:2.4.1:test
[INFO] | +- org.junit.jupiter:junit-jupiter:jar:5.7.0:test
[INFO] | | +- org.junit.jupiter:junit-jupiter-params:jar:5.7.0:test
[INFO] | | \- org.junit.jupiter:junit-jupiter-engine:jar:5.7.0:test
[INFO] | | \- org.junit.platform:junit-platform-engine:jar:1.7.0:test