我正在尝试模拟列表
counted[binModel.getX() - 1][binModel.getY() - 1].add(activeModel);
add
是boolean
,我正在尝试这个
Mockito.when(testee.getCounted()[binModel.getSelectionX() - 1]
[binModel.getSelectionY() - 1].add(activeModel)).thenReturn(Mockito.anyBoolean());
这会在
上引发错误public List<CountModel>[][] getCounted() {
return counted.clone();
}
counted
在原始类中声明为
private List<CountModel>[][] counted;
这是错误:
空指针和@mock private List [] []计数; Mockito不能模仿/间谍: - 最终类 - 匿名类 - 原始类型
答案 0 :(得分:3)
数组是java中的最后一个类,因此不能被模拟。
您可能需要做的就是自己填充数组,例如......
// Lose the @Mock annotation if that's how you set it up
private List<CountModel>[][] counted;
@Before
@SuppressWarnings("unchecked")
public void setup() {
counted = new List[X_SIZE][Y_SIZE];
for(int x = 0; x < X_SIZE; x++) {
for(int y = 0; y < Y_SIZE; y++) {
counted[x][y] = mock(List.class);
}
}
}
(不知道为什么你真的想要模拟一个列表)
要绕过“克隆”的事情,您可以构建另一个模拟来表示克隆列表,或者只返回相同的列表......
@Before
@SuppressWarnings("unchecked")
public void setup() {
counted = new List[X_SIZE][Y_SIZE];
for(int x = 0; x < X_SIZE; x++) {
for(int y = 0; y < Y_SIZE; y++) {
counted[x][y] = mock(List.class);
when(counted[x][y].clone()).thenReturn(counted[x][y]);
}
}
}
就像我说的那样,模拟列表可能不是模拟的最好用法。也许创建一个真实的列表,其中包含模拟“CountModels”或其他东西?
(您可能需要发布完整的代码,以便让我更多地了解您的目标)