[英]Mockito doReturn making list immutable
我正在嘗試測試這樣的方法:
public getIndustries(int countryId) {
var industryList = industryControllerService.getIndustriesByCountryId(countryId);
...
industryList.add(new IndustryCategory(otherIndustry));
}
在我的測試中我有:
List<IndustryCategory> expected = List.of(industryCategory1, industryCategory2);
doReturn(expected).when(industryControllerService).getIndustriesByCountryId(anyInt());
...
但出於某種原因,Mockito 將 industryList 作為不可變集合返回,因此添加操作會拋出 UnsupportedOperationException。
我怎樣才能改變它,使列表成為一個可以編輯的普通列表?
List.of
返回一個不可變列表。 嘗試將其復制到 ArrayList: expected = new ArrayList<>(List.of(...))
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.