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

Closed
Fival85 opened this issue Jan 3, 2019 · 1 comment
Closed
Assignees
Labels

Comments

@Fival85
Copy link

@Fival85 Fival85 commented Jan 3, 2019

Please provide the following information:

  • Version of JMockit that was used:
    1.44

  • Description of the problem:
    When I use a mocked list of anything, LocalDateTime.now() 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) { LocalDateTime.now(); Optional<String> any = Arrays.asList("a").stream().findAny(); }

If you comment out LocalDateTime.now() or the mocked List ... no exception occurs

I use openjdk11 and inteliJ as IDE.

Could anybody explain this for me?
Greets
Dennis

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

This comment has been minimized.

Copy link
Member

@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.
2 participants
You can’t perform that action at this time.