使用 Mockito 模拟静态

发布于 2025-01-15 22:22:15 字数 1286 浏览 1 评论 0原文

我的测试文件中有以下声明,当我运行测试时,这两行中的第一行失败。

@BeforeAll
public static void beforeAll() {
    loggerFactoryMockedStatic = mockStatic(LoggerFactory.class);
    loggerFactoryMockedStatic.when(() -> LoggerFactory.getLogger(PipelineExecutionService.class))
            .thenReturn(logger);
}

我得到的错误是这样的:

org.mockito.exceptions.base.MockitoException: 
The used MockMaker SubclassByteBuddyMockMaker does not support 
the creation of static mocks
Mockito's inline mock maker supports static mocks based on the Instrumentation API.
You can simply enable this mock mode, by placing the 'mockito-inline' artifact where 
you are currently using 'mockito-core'.
Note that Mockito's inline mock maker is not supported on Android.

所以,我将 POM 文件的一部分从 更改

    <dependency>
        <groupId>org.mockito</groupId>
        <artifactId>mockito-core</artifactId>
        <scope>test</scope>
    </dependency>

    <dependency>
        <groupId>org.mockito</groupId>
        <artifactId>mockito-inline</artifactId>
        <scope>test</scope>
    </dependency>

但是,运行测试后我仍然收到此错误。知道为什么会这样吗?

I have the following declaration in my test file and the first of these two lines is failing when I run my tests.

@BeforeAll
public static void beforeAll() {
    loggerFactoryMockedStatic = mockStatic(LoggerFactory.class);
    loggerFactoryMockedStatic.when(() -> LoggerFactory.getLogger(PipelineExecutionService.class))
            .thenReturn(logger);
}

The error I get is this:

org.mockito.exceptions.base.MockitoException: 
The used MockMaker SubclassByteBuddyMockMaker does not support 
the creation of static mocks
Mockito's inline mock maker supports static mocks based on the Instrumentation API.
You can simply enable this mock mode, by placing the 'mockito-inline' artifact where 
you are currently using 'mockito-core'.
Note that Mockito's inline mock maker is not supported on Android.

So, I changed part of my POM file from

    <dependency>
        <groupId>org.mockito</groupId>
        <artifactId>mockito-core</artifactId>
        <scope>test</scope>
    </dependency>

to

    <dependency>
        <groupId>org.mockito</groupId>
        <artifactId>mockito-inline</artifactId>
        <scope>test</scope>
    </dependency>

But, after running my tests I still get this error. Any idea why this might be?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

赤濁 2025-01-22 22:22:15

问题来自Intelij。在cmd中测试工作正常。为了解决这个问题,请执行以下步骤:

在测试资源中创建目录mockito-extensions,并在该目录中创建文件org.mockito.plugins.MockMaker。完整路径应如下所示:src/test/resources/mockito-extensions/org.mockito.plugins.MockMaker。

在文件中,添加以下行mock-maker-inline

在此处输入图像描述

注意:两个依赖项都是必需的
输入图片此处描述

The problem comes from Intelij. In cmd the test is working fine. In order to overcome this issue, do the followings steps:

In test resources create the directory mockito-extensions and in that directory the file org.mockito.plugins.MockMaker. The complete path should look like this: src/test/resources/mockito-extensions/org.mockito.plugins.MockMaker.

In the file, add the following line mock-maker-inline

enter image description here

Note: both dependencies are needed
enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文