fg78nc
fg78nc

Reputation: 5232

Mockito does not initialize mock in test running with JUnit 5 in @BeforeAll annotated method

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

Upvotes: 19

Views: 23456

Answers (3)

Builder Bob
Builder Bob

Reputation: 149

it's a bit old but I found a solution that works for me :

public class MockitoExtensionBeforeAll extends MockitoExtension implements BeforeAllCallback, AfterAllCallback {

@Override
public void beforeAll(ExtensionContext context) {
    super.beforeEach(context);
}

@Override
public void afterAll(ExtensionContext context) {
    super.afterEach(context);
}

@Override
public void beforeEach(ExtensionContext context) {

}

@Override
public void afterEach(ExtensionContext context) {

}

}

Upvotes: -2

Mureinik
Mureinik

Reputation: 311823

You're missing the initialization of the myMockedClass. Note that you can't use a @Mock annotation for it, because the @BeforeAll method would be run before that annotation is used to initialize the mocked object, and you'd have to resort to explicitly calling Mockito.mock:

@BeforeAll
public void init() {
    myMockedClass = mock(MyMockedClass.class); // Here
    when(myMockedClass.getSomething()).thenReturn(something);
}

Upvotes: 7

Thomas Kläger
Thomas Kläger

Reputation: 21510

The MockitoExtension class implements the BeforeEachCallback but not the BeforeAllCallback from JUnit-Jupiter-API. It does therefore not provide any additional behaviour for @BeforeAll annotated methods.

Source extract of MockitoExtension

public class MockitoExtension implements BeforeEachCallback, AfterEachCallback, ParameterResolver {

Upvotes: 17

Related Questions