使用 Mockito 模拟静态
我的测试文件中有以下声明,当我运行测试时,这两行中的第一行失败。
@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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题来自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
Note: both dependencies are needed
