【问题标题】:Powermock throws ClassNotPreparedException when using JUnit 5使用 JUnit 5 时 Powermock 抛出 ClassNotPreparedException
【发布时间】:2019-01-17 15:51:11
【问题描述】:

我有一个示例测试类,我想在其中模拟一个静态类。我的 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

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

【问题讨论】:

标签: unit-testing mockito powermock junit5 junit-jupiter


【解决方案1】:

MockitoExtension 不支持 Powermock。

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

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

【讨论】:

  • 我在这个文档中没有看到任何东西展示了如何与 JUnit 5 集成。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-12-12
  • 1970-01-01
  • 2017-02-09
  • 1970-01-01
  • 1970-01-01
  • 2017-03-09
相关资源
最近更新 更多