mockito - Mockito 3.7:MockStatic 失败并显示消息:0 Matchers Expected, 1 Recorded InvalidUseOfMatchersException
问题描述
我试图在 Mockito 中模拟一个静态函数,如下所示:
@ExtendWith(MockitoExtension.class)
public class MyTest {
MockedStatic<MyStaticClass> myStaticClassMock;
@Mock
MyClient myClientMock;
@BeforeEach
public void setup() {
myStaticClassMock= Mockito.mockStatic(MyStaticClass.class);
myStaticClassMock.when(() -> MyStaticClass.getClient()).thenReturn(this.myClientMock);
...
}
@Test
public void someTest() {
...
this.classUnderTest.someFunction(); //Error here because MyStaticClass.getClient() call fails.
...
}
在运行我的测试类时,我最终InvalidUseOfMatchersException: 0 Matchers Expected, 1 Recorded...
会MyStaticClass.getClient()
调用我正在测试的函数。更奇怪的是,如果我在我的 IDE 中调试测试,测试将通过并且模拟的静态函数将正确返回模拟。
这是使用 JDK 11、Mockito 3.7.7 编写的。有什么我做错了吗?
解决方案
推荐阅读
- php - Laravel:对多个图像进行图像验证不起作用
- dart - 如何在 Flutter 的 TextField 中添加遮罩?
- azure - 在 Azure 中基于 Web 发起创建 IaaS
- google-sheets - 基于 if/then 在多个字段上的条件格式
- wix - 使用自定义操作的 REG_MULTI_SZ 的 MsiSetProperty
- javascript - 以后是否可以将值绑定到函数中的 Vue 元素?
- plot - Jzy3d 3d插值图不渲染
- excel - 如果所有三个条件都完全满足,那么“ok”
- python - 在 Python 中处理嵌套的 JSON
- r - 如何在 R 中使用 ggplot2 将 y 轴更改为百分比?