java - 如何将参数传递给模拟类方法?
问题描述
我正在使用 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());
任何人都可以帮忙吗?
解决方案
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")
如果您的模拟应该只对特定值做出反应,请使用。
推荐阅读
- python - 错误类型错误:无法将 dtyped [float64] 数组与 [bool] 类型的标量进行比较
- c# - Visual Studio 2019 远程调试器不会因异常而中断
- python-3.x - 如何填充重新组合的行中的值
- javascript - 在 TypeScript 中创建一个双精度数组并填充它
- openstreetmap - 将打开的街道地图数据库转换为栅格属性表
- flutter - list.addAll() 函数对一个列表不起作用,对其他列表起作用
- google-maps - 页面上有多个带有标记的 Google 地图
- php - php codeigniter中的单个数组到关联数组(组合数组)
- elasticsearch - Elasticsearch 将文档字段值从整数更新为字符串
- linux - 多个进程的多个中断向量表