使用 Mockito 模拟静态

问题描述 投票:0回答:1

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

@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>

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

java junit mockito
1个回答
0
投票

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

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

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

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.