Powermock 在使用 JUnit 5 时抛出 ClassNotPreparedException

Pri*_*iya 2 unit-testing mockito powermock junit5 junit-jupiter

我有一个示例测试类,我想在其中模拟一个静态类。我的 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'`
Run Code Online (Sandbox Code Playgroud)

当我尝试我的测试用例时

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

@Test
public void shouldCheckIfSyncTimeIsAboveThreshold() {
    mockStatic(MyUtil.class);
    when(MyUtil.getValue()).thenReturn(5));
 }
Run Code Online (Sandbox Code Playgroud)

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

org.powermock.api.mockito.ClassNotPreparedException:
The class MyUtil not prepared for test
Run Code Online (Sandbox Code Playgroud)

有什么办法可以让我达到同样的目标

Sam*_*nen 5

MockitoExtension不支持Powermock。

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

有关如何使用 Powermock 的详细信息,请访问项目网页

  • 我在本文档中没有看到任何内容说明如何与 JUnit 5 集成。 (3认同)