Reputation: 1896
MyStreamClass mock = mock(MyStreamClass.class);
when(mock.streamMethod()).thenReturn(Stream.of("A", "B"));
System.out.println(""+mock.streamMethod().findFirst());
System.out.println(""+mock.streamMethod().findFirst());
the second call of findFirst will throw java.lang.IllegalStateException: stream has already been operated upon or closed
Upvotes: 13
Views: 37601
Reputation: 111
Try thenAnswer Using Java 8 Approach
when(mock.streamMethod()).thenAnswer(invocation -> Stream.of("A", "B"))
Upvotes: 5
Reputation: 26522
Try the thenAnswer
instead of thenReturn
:
Answer<Stream> answer = new Answer<Stream>() {
public Stream answer(InvocationOnMock invocation) throws Throwable {
return Stream.of("A", "B");
}
};
when(mock.streamMethod()).thenAnswer(answer);
Now a new Stream will be created for each call to streamMethod
.
Further read:
Here is an article I wrote on Dynamic Answering in Mockito that might be complementary.
Upvotes: 24
Reputation: 14999
You're not mocking a Stream, you're creating one - and only one, which will be consumed after the first terminating method having been called on it, which is what you experience.
Most of the time, it's best to stick with mocking as far as possible, in your case
MyStreamClass mock = mock(MyStreamClass.class);
Stream mockStream = mock(Stream.class);
when(mock.streamMethod()).thenReturn(mockStream);
That should be enough for testing all clients of MyStreamClass
- there's no point in getting actual results from the stream.
If that does not fit your design, you can alternatively use an Answer
:
when(mock.streamMethod()).then(i -> Stream.of("A", "B"));
this will result in the stream being created every time the method is called.
Or, you can mock several calls with thenReturn()
.
when(mock.streamMethod()).thenReturn(Stream.of("A", "B"), Stream.of("A", "B"));
which will last you 2 calls.
Upvotes: 9
Reputation: 746
Try this:
MyStreamClass mock = Mockito.mock(MyStreamClass.class);
Mockito.when(mock.streamMethod()).thenReturn(Stream.of("A", "B"));
Stream s = mock.streamMethod();
s.forEach(System.out::println);
Upvotes: 0