首页 > 解决方案 > 如何将参数传递给模拟类方法?

问题描述

我正在使用 mockito 来测试 mu jdk11-springboot 应用程序。

我的应用程序有类“ClientRepository”,并且有一个名为“findById”的方法,它采用 UUID 类型的参数。

所以方法看起来像:

 public String findById(UUID id)

现在我嘲笑这个类来测试:

@MockBean
private ClientRepository clientRepo;

现在我想弄清楚如何在这里传递 UUID 参数:

 Mockito.when(clientRepo.findById(UUID id))
            .thenReturn(dslContext.selectFrom(CLIENT).where(CLIENT.ID.eq(UUID.fromString("3e064b19-ef76-4aea-bf82-e9d8d01daf1c"))).fetch());

任何人都可以帮忙吗?

标签: javamockitopowermockito

解决方案


 Mockito.when(clientRepo.findById(Mockito.any(UUID.class))
            .thenReturn(dslContext.selectFrom(CLIENT).where(CLIENT.ID.eq(UUID.fromString("3e064b19-ef76-4aea-bf82-e9d8d01daf1c"))).fetch());

Mockito.eq("your-uuid-goes-here")如果您的模拟应该只对特定值做出反应,请使用。


推荐阅读