簡體   English   中英

Mockito doReturn 使列表不可變

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM