Mockito 不会在带@BeforeAll
注释的方法中使用 JUnit 5 初始化模拟运行。如果我将init
's 方法注释更改为@BeforeEach
. 测试在 IntelliJ IDEA 中运行。
我的测试课:
@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
依赖关系(仅显示相关的,为简洁起见省略其他):
[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