【问题标题】:How to mock a static method in JUnit 5如何在 JUnit 5 中模拟静态方法
【发布时间】:2019-03-05 04:33:33
【问题描述】:

我正在将我的 JUnit 升级到版本 5,当我运行 JUnit 5 时出现此错误

我在我的 pom 中使用

<dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter-api</artifactId>
</dependency>
<dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter-engine</artifactId>
</dependency>

org.powermock.api.mockito.ClassNotPreparedException:

[Ljava.lang.Object;@723ca036 com.xxxxxx.MyClass 类没有准备好 进行测试。

我使用@RunWith(JUnitPlatform.class) 作为我的课堂测试符号

我的代码是

PowerMockito.mockStatic(MyClass.class);
when(MyClass.get(anyString()))
    .thenReturn(mock);

【问题讨论】:

标签: junit junit5


【解决方案1】:

您必须使用 ExtendWith。在junit 5中,注释将@RunWith更改为

@ExtendWith(JUnitPlatform.class)

Further details on how to use Extend with

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多