Powermock 在使用 JUnit 5 时抛出 ClassNotPreparedException

Powermock throws ClassNotPreparedException when using JUnit 5

我有一个示例测试 class 我想在其中模拟静态 class.My build.gradle 就像

testCompile group: 'org.junit.jupiter', name: 'junit-jupiter-api', version: '5.2.0'
testCompile group: 'org.junit.jupiter', name: 'junit-jupiter-params', version: '5.2.0'
testRuntime group: 'org.junit.jupiter', name: 'junit-jupiter-engine', version: '5.2.0'
testRuntime("org.junit.platform:junit-platform-launcher:1.1.1")

testCompile group: 'org.powermock', name: 'powermock-api-mockito2', version: '2.0.0-beta.5'
testCompile group: 'org.mockito', name: 'mockito-core', version: '2.21.0'
testCompile group: 'org.mockito', name: 'mockito-junit-jupiter', version: '2.21.0'`

当我尝试我的测试用例时

@ExtendWith(MockitoExtension.class)
@PrepareForTest(MyUtil.class)
public class MyTest {

@Test
public void shouldCheckIfSyncTimeIsAboveThreshold() {
    mockStatic(MyUtil.class);
    when(MyUtil.getValue()).thenReturn(5));
 }

但是当我 运行 这个时,我得到了像

这样的异常
org.powermock.api.mockito.ClassNotPreparedException:
The class MyUtil not prepared for test

有什么方法可以达到同样的效果

MockitoExtension 不支持 Powermock。

因此,没有注册的扩展可以处理 @PrepareForTest(MyUtil.class)

有关如何使用 Powermock 的详细信息,请访问 project webpage