Mock a method that returns a Stream and is called more than one time
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.
Try thenAnswer Using Java 8 Approach
when(mock.streamMethod()).thenAnswer(invocation -> Stream.of("A", "B"))
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.