Mock a method that returns a Stream and is called more than one time
问题 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 回答1: Try the thenAnswer instead of thenReturn : Answer<Stream> answer = new Answer<Stream>() { public Stream answer(InvocationOnMock invocation) throws