spring 基于特定属性的匹配器

wecizke3  于 2022-10-30  发布在  Spring
关注(0)|答案(1)|浏览(229)

我有一个带有executeOperation方法的operationService类,它接受一个数据库请求对象。这个数据库请求对象有4个属性,其中一个是operation。我想在基于数据库请求.operation字段调用operationService.executeOperation时返回不同的响应。请帮助我。这是我正在尝试实现的目标

when(operationService.executeOperation(anyDatabaserequest object with operation as 'DEMO')).thenReturn('DEMO_RESPONSE');
 when(operationService.executeOperation(anyDatabaserequest object with operation as 'PILOT')).thenReturn('PILOT_RESPONSE');
0g0grzrc

0g0grzrc1#

您可以使用Mockito.argThat执行以下操作:

Mockito.when(obj.testMethod(Mockito.argThat(x -> "input1".equals(x)))).thenReturn("whatever");
Mockito.when(obj.testMethod(Mockito.argThat(x -> "input2".equals(x)))).thenReturn("whatever2");

var substring = obj.testMethod("input1"); //this is "whatever"
var substring2 = obj.testMethod("input2"); //this is "whatever2"

相关问题