简体   繁体   English

使用多个任何参数定义mockito

[英]define mockito when with multiple any arguments

I am trying to define when mockito method with multiple any arguments: 我试图定义when的Mockito方法与多个any参数:

TestBedDaoClient testBedDaoClient = mock(TestBedDaoClient.class);
when(testBedDaoClient.addTestBed(anyString(), anyString(), any(VCloudConfiguration.class))).thenReturn(testBedPojoMockData);

In target test class: 在目标测试类中:

TestBedPojo addedTestBedPojo = testBedDaoClient.addTestBed(testBedName, testBedDescription, vCloudConfiguration);

In DAO client: 在DAO客户端:

public TestBedPojo addTestBed(String testBedName, String testBedDescription, VCloudConfiguration vCloudConfiguration){
     return testBedPojo;
}

I wanted to define when in such a way that it returns testBedPojoMockData with any values of arguments. 我想以这样的方式定义when返回带有任何参数值的testBedPojoMockData But I am getting error: Argument(s) are different! 但我得到错误: Argument(s) are different!

I even tried: 我甚至尝试过:

when(testBedDaoClient.addTestBed("test", "test", any(VCloudConfiguration.class))).thenReturn(testBedPojoMockData);
when(testBedDaoClient.addTestBed(any(), any(), any())).thenReturn(testBedPojoMockData);

But no luck. 但没有运气。 How I can define this when so that it returns the mock data on any call? 如何在任何调用when返回模拟数据when如何定义?

The correct combination of when and verify should be used. 应该使用whenverify的正确组合。 It's failing on any other combination of argument in addTestBed method. 它在addTestBed方法中的任何其他参数组合都失败了。

when(testBedDaoClient.addTestBed(anyString(), anyString(), any(VCloudConfiguration.class))).thenReturn(testBedPojoMockData);
//calling target method
verify(testBedDaoClient, times(1)).addTestBed(anyString(), anyString(), any(VCloudConfiguration.class));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM