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

In jdk19 Cannot mock/spy class java.util.concurrent.ThreadLocalRandom #2985

Closed
sushikarwasra opened this issue Apr 24, 2023 · 1 comment
Closed

Comments

@sushikarwasra
Copy link

Hi, i am getting below error when i am using jdk19, i tried using --add-opens=java.base/java.util.concurrent=ALL-UNNAMED but that also not helping resolve below issue, any suggestion how i can mock ThreadLocalRandom when using jdk19, i am not getting any error if i use jdk17 or below version. Below is the code that generates this error.
final ThreadLocalRandom random = Mockito.mock(ThreadLocalRandom.class);
Mockito.when(random.nextInt(Mockito.anyInt())).thenReturn(0);

[ERROR] Failures:
[ERROR] Mockito
Cannot mock/spy class java.util.concurrent.ThreadLocalRandom
Mockito cannot mock/spy following:

  • final classes
  • anonymous classes
  • primitive types
@TimvdLippe
Copy link
Contributor

Unfortunately, ThreadLocalRandom is one of the classes that Mockito relies on internally for its behavior. Stubbing ThreadLocalRandom will therefore lead to undefined behavior. Additionally, it is advised not to mock classes you don't own: https://github.com/mockito/mockito/wiki/How-to-write-good-tests#dont-mock-a-type-you-dont-own We are working on improving the user experience by working on a DoNotMock feature to avoid mocking classes/methods that are known to crash Mockito internals (#1833). Therefore, I am closing this as "Infeasible". Apologies for the uninformative exception that is thrown.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants