首页 > 解决方案 > 方法抛出 'org.mockito.exceptions.misusing.InvalidUseOfMatchersException' 异常。

问题描述

我正在尝试测试此方法,但是出现以下错误:

Method threw 'org.mockito.exceptions.misusing.InvalidUseOfMatchersException' exception. 

在这行代码上:

    when(tester.method(
            any(String.class), any(LocalDate.class), any(boolean.class),any(boolean.class), any(String.class))).thenReturn(item);

标签: unit-testingtestingjunit

解决方案


对于您使用的大多数类型,已经有内置的匹配器。

when(tester.method(anyString(), any(LocalDate.class), anyBoolean(),anyBoolean(), anyString()))
.thenReturn(item);

参考类 ArgumentMatchers

对于原始类型,请使用any{Type}()族。


推荐阅读