【发布时间】:2011-12-23 12:18:07
【问题描述】:
这是我的问题:
public interface Containter extends ModelElement{
List<? extends ModelElement> getChildren();
}
有几个类实现了容器,我想模拟它们:
public class MockMama {
public static <T extends Containter, Y extends ModelElement> T bornContainer(Class<T> clazz, Y ... children) {
T container = mock(clazz);
when(container.getChildren()).thenReturn(Arrays.asList(children));
return container;
}
}
但这不起作用。 Eclipse 说“OngoingStubbing> 类型中的 thenReturn(List) 方法不适用于参数 (List)”。我还尝试将 List<? extends ModelElement> 类型的本地声明变量传递给 thenReturn 但这也无济于事。
非常感谢和欢迎任何帮助:)
【问题讨论】:
-
好的,看起来可以通过引入额外的本地变量来修复它,比如 List list = Arrays.asList(children); when(container.getChildren()).thenReturn(list);有什么方法可以做到不引起原始类型警告?