小编drW*_*son的帖子

如何模拟通用通配符的返回值

我试图在Java 8中了解泛型和通配符.但我无法理解为什么不能模拟这个存储库方法.代码非常简单,因此应该很容易重现.

我在"thenReturn"部分的"when"中得到了这个编译错误

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>)
Run Code Online (Sandbox Code Playgroud)

考试:

@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);     
}
Run Code Online (Sandbox Code Playgroud)

班级:

public class Something {}
Run Code Online (Sandbox Code Playgroud)

存储库:

public interface SomeRepository{
    Stream<? extends Something> getStream();
}
Run Code Online (Sandbox Code Playgroud)

有人可以帮忙吗?谢谢!

java wildcard mockito java-8 java-stream

1
推荐指数
1
解决办法
428
查看次数

标签 统计

java ×1

java-8 ×1

java-stream ×1

mockito ×1

wildcard ×1