简体   繁体   中英

define mockito when with multiple any arguments

I am trying to define when mockito method with multiple any arguments:

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:

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. But I am getting error: 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?

The correct combination of when and verify should be used. It's failing on any other combination of argument in addTestBed method.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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