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

Mockito Test failure on Java 17: "cannot mock this class: class java.util.Random" #664

Closed
brenuart opened this issue Sep 25, 2021 · 0 comments · Fixed by #665
Closed

Mockito Test failure on Java 17: "cannot mock this class: class java.util.Random" #664

brenuart opened this issue Sep 25, 2021 · 0 comments · Fixed by #665
Labels
Milestone

Comments

@brenuart
Copy link
Collaborator

Some Mockito tests are failing under Java 17 with the following error:

[INFO] Running net.logstash.logback.appender.destination.RandomDestinationConnectionStrategyTest
Error:  Tests run: 4, Failures: 0, Errors: 4, Skipped: 0, Time elapsed: 0.034 s <<< FAILURE! - in net.logstash.logback.appender.destination.RandomDestinationConnectionStrategyTest
Error:  testConnectionTtl_success  Time elapsed: 0.011 s  <<< ERROR!
org.mockito.exceptions.base.MockitoException: 

Mockito cannot mock this class: class java.util.Random.

Mockito can only mock non-private & non-final classes.
If you're not sure why you're getting this error, please report to the mailing list.

Java               : 17
JVM vendor name    : Eclipse Adoptium
JVM vendor version : 17+35
JVM name           : OpenJDK 64-Bit Server VM
JVM version        : 17+35
JVM info           : mixed mode, sharing
OS name            : Linux
OS version         : 5.8.0-1041-azure

Underlying exception : java.lang.IllegalStateException: Cannot access annotation property public abstract java.lang.String jdk.internal.util.random.RandomSupport$RandomGeneratorProperties.name()
Caused by: java.lang.IllegalStateException: Cannot access annotation property public abstract java.lang.String jdk.internal.util.random.RandomSupport$RandomGeneratorProperties.name()
Caused by: java.lang.IllegalAccessException: class net.bytebuddy.description.annotation.AnnotationDescription$ForLoadedAnnotation cannot access interface jdk.internal.util.random.RandomSupport$RandomGeneratorProperties (in module java.base) because module java.base does not export jdk.internal.util.random to unnamed module @1ea0fbbc
brenuart added a commit that referenced this issue Sep 26, 2021
…sues during test

Cfr Issue #664

Instead of exposing and mocking the Random used by the strategy, hide it behind a `nextInt()` member method that can safely by intercepted by Mockito during tests.
@brenuart brenuart linked a pull request Sep 26, 2021 that will close this issue
@philsttr philsttr added this to the 7.0 milestone Nov 13, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants