SpringData findAll(Pageable) 上的 Mockito 不匹配方法签名

Mockito mismatch method signature on SpringData findAll(Pageable)

这令人费解,奇怪的是我的搜索没有找到任何答案。 我有一个非常简单的 SpringData Mongo 存储库,我正在尝试使用此类存储库测试控制器。控制器调用存储库上的 findAll(Pageable),因此我希望使用 Mockito 模拟此类调用:

Page<Idea> page = new PageImpl<Idea>(
            IntStream.range(1, 10)
            .mapToObj(i -> Idea.builder().title("idea-" + i).build())
            .collect(Collectors.toList()));

when(repo.findAll(any(Pageable.class))).thenReturn(page);

但这会导致令人费解的编译错误:

The method findAll(Sort) in the type MongoRepository is not applicable for the arguments (Matcher<Pageable>)

显然 Mockito 选择了错误的方法,但为什么呢? Pageable 是一个接口,它不扩展 Sort!我还尝试在存储库上定义一个 findAll(Pageable) 抽象方法,但它随后报告 class 不匹配:Matcher<Pageable>(调用)与 Pageable(定义)不匹配。

我迷路了...

哇哦!找到了一个解决方案,可能其他人会觉得这个有用。

罪魁祸首是我使用的匹配器方法:我上面使用的 any() 来自 org.hamcrest.Matchers,而不是 org.mockito.Matchers。我相信我的 IDE 没有选择正确的,因为后者现在已被弃用,取而代之的是 org.mockito.ArgumentMatchers