![](/img/trans.png)
[英]Mockito stubbing returning null when any Any arguments are used for method
[英]define mockito when with multiple any arguments
我試圖定義when
的Mockito方法與多個any
參數:
TestBedDaoClient testBedDaoClient = mock(TestBedDaoClient.class);
when(testBedDaoClient.addTestBed(anyString(), anyString(), any(VCloudConfiguration.class))).thenReturn(testBedPojoMockData);
在目標測試類中:
TestBedPojo addedTestBedPojo = testBedDaoClient.addTestBed(testBedName, testBedDescription, vCloudConfiguration);
在DAO客戶端:
public TestBedPojo addTestBed(String testBedName, String testBedDescription, VCloudConfiguration vCloudConfiguration){
return testBedPojo;
}
我想以這樣的方式定義when
返回帶有任何參數值的testBedPojoMockData
。 但我得到錯誤: Argument(s) are different!
我甚至嘗試過:
when(testBedDaoClient.addTestBed("test", "test", any(VCloudConfiguration.class))).thenReturn(testBedPojoMockData);
when(testBedDaoClient.addTestBed(any(), any(), any())).thenReturn(testBedPojoMockData);
但沒有運氣。 如何在任何調用when
返回模擬數據when
如何定義?
應該使用when
和verify
的正確組合。 它在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.