【发布时间】:2016-12-07 10:48:01
【问题描述】:
我试图了解 java 8 中的泛型和通配符。但我不明白为什么不能模拟这个存储库方法。 代码很简单,应该很容易复现。
我在“when”的“thenReturn”部分遇到了这个编译错误
The method thenReturn(Stream<capture#1-of ? extends Something>) in the type
OngoingStubbing<Stream<capture#1-of ? extends Something>> is not applicable
for the arguments (Stream<capture#3-of ? extends Something>)
测试:
@Test
public void ItShourReturnStreamFromRepository() {
List<Something> list = new ArrayList<Something>();
list.add(new Something());
Stream<? extends Something> stream = list.stream();
when(someRepository.getStream()).thenReturn(stream);
}
班级:
public class Something {}
存储库:
public interface SomeRepository{
Stream<? extends Something> getStream();
}
有人可以帮忙吗? 谢谢!
【问题讨论】:
-
遵循the official tutorial的建议:应避免使用通配符作为返回类型……
-
在界面中添加一些类型怎么样?
interface SomeRepository<T extends Something> { Stream<T> getStream(); }
标签: java java-8 mockito wildcard java-stream