Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

java.lang.IllegalStateException: stream has already been operated upon or closed #573

Fival85 opened this issue Jan 3, 2019 · 1 comment


Copy link

@Fival85 Fival85 commented Jan 3, 2019

Please provide the following information:

  • Version of JMockit that was used:

  • Description of the problem:
    When I use a mocked list of anything, and make a stream out of a list, an exception occured:
    java.lang.IllegalStateException: stream has already been operated upon or closed

Here is a minimal code example (junit-test)
@Test public void whyThisError(@Mocked List<Integer> doNothingWith) {; Optional<String> any = Arrays.asList("a").stream().findAny(); }

If you comment out or the mocked List ... no exception occurs

I use openjdk11 and inteliJ as IDE.

Could anybody explain this for me?

@rliesenfeld rliesenfeld self-assigned this Jan 3, 2019
Copy link

@rliesenfeld rliesenfeld commented Jan 3, 2019

It happens because (in Java 8+) interfaces such as List and Collection have methods with implementations, which are being mocked due to the @Mocked List, affecting all List instances.

So, to avoid unexpected failures like this, said interfaces should be (and will be) regarded as unmockable.

@jmockit jmockit locked and limited conversation to collaborators Apr 18, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

No branches or pull requests

2 participants