Closed anshupal closed 2 years ago
Hi @anshupal, good finding! It's an error caused by some improvements in the JDK. You can find how to fix it and the background in one of the book's online extra chapters: https://thepracticaldeveloper.com/book-update-2.5.5/#no-more-spying-on-randoms
If you're liking the book, please rate it on Amazon and/or Goodreads, thanks!
Hi- Getting the following error while running the ChallengeGeneratorServiceTest .
org.mockito.exceptions.base.MockitoException: Unable to initialize @Spy annotated field 'random'.
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 : 18 JVM vendor name : Oracle Corporation JVM vendor version : 18.0.1.1+2-6 JVM name : OpenJDK 64-Bit Server VM JVM version : 18.0.1.1+2-6 JVM info : mixed mode, sharing OS name : Windows 10 OS version : 10.0
Underlying exception : java.lang.IllegalStateException: Cannot access annotation property public abstract java.lang.String jdk.internal.util.random.RandomSupport$RandomGeneratorProperties.name()
Caused by: 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 : 18 JVM vendor name : Oracle Corporation JVM vendor version : 18.0.1.1+2-6 JVM name : OpenJDK 64-Bit Server VM JVM version : 18.0.1.1+2-6 JVM info : mixed mode, sharing OS name : Windows 10 OS version : 10.0
Underlying exception : java.lang.IllegalStateException: Cannot access annotation property public abstract java.lang.String jdk.internal.util.random.RandomSupport$RandomGeneratorProperties.name() ... 54 more Caused by: java.lang.IllegalStateException: Cannot access annotation property public abstract java.lang.String jdk.internal.util.random.RandomSupport$RandomGeneratorProperties.name() at net.bytebuddy.description.annotation.AnnotationDescription$ForLoadedAnnotation.getValue(AnnotationDescription.java:824) at net.bytebuddy.implementation.attribute.AnnotationAppender$Default.handle(AnnotationAppender.java:314) at net.bytebuddy.implementation.attribute.AnnotationAppender$Default.doAppend(AnnotationAppender.java:377) at net.bytebuddy.implementation.attribute.AnnotationAppender$Default.append(AnnotationAppender.java:354) at net.bytebuddy.implementation.attribute.TypeAttributeAppender$ForInstrumentedType.apply(TypeAttributeAppender.java:93) at net.bytebuddy.dynamic.scaffold.TypeWriter$Default$ForCreation.create(TypeWriter.java:5916) at net.bytebuddy.dynamic.scaffold.TypeWriter$Default.make(TypeWriter.java:2213) at net.bytebuddy.dynamic.scaffold.subclass.SubclassDynamicTypeBuilder.make(SubclassDynamicTypeBuilder.java:232) at net.bytebuddy.dynamic.scaffold.subclass.SubclassDynamicTypeBuilder.make(SubclassDynamicTypeBuilder.java:204) at net.bytebuddy.dynamic.DynamicType$Builder$AbstractBase.make(DynamicType.java:3668) at org.mockito.internal.creation.bytebuddy.SubclassBytecodeGenerator.mockClass(SubclassBytecodeGenerator.java:288) at org.mockito.internal.creation.bytebuddy.TypeCachingBytecodeGenerator.lambda$mockClass$0(TypeCachingBytecodeGenerator.java:47) at net.bytebuddy.TypeCache.findOrInsert(TypeCache.java:168) at net.bytebuddy.TypeCache$WithInlineExpunction.findOrInsert(TypeCache.java:399) at net.bytebuddy.TypeCache.findOrInsert(TypeCache.java:190) at net.bytebuddy.TypeCache$WithInlineExpunction.findOrInsert(TypeCache.java:410) at org.mockito.internal.creation.bytebuddy.TypeCachingBytecodeGenerator.mockClass(TypeCachingBytecodeGenerator.java:40) at org.mockito.internal.creation.bytebuddy.SubclassByteBuddyMockMaker.createMockType(SubclassByteBuddyMockMaker.java:77) at org.mockito.internal.creation.bytebuddy.SubclassByteBuddyMockMaker.createMock(SubclassByteBuddyMockMaker.java:43) at org.mockito.internal.creation.bytebuddy.ByteBuddyMockMaker.createMock(ByteBuddyMockMaker.java:42) at org.mockito.internal.util.MockUtil.createMock(MockUtil.java:53) at org.mockito.internal.MockitoCore.mock(MockitoCore.java:96) at org.mockito.Mockito.mock(Mockito.java:1965) at org.mockito.internal.configuration.SpyAnnotationEngine.spyNewInstance(SpyAnnotationEngine.java:130) at org.mockito.internal.configuration.SpyAnnotationEngine.process(SpyAnnotationEngine.java:70) at org.mockito.internal.configuration.InjectingAnnotationEngine.processIndependentAnnotations(InjectingAnnotationEngine.java:75) at org.mockito.internal.configuration.InjectingAnnotationEngine.process(InjectingAnnotationEngine.java:47) at org.mockito.MockitoAnnotations.openMocks(MockitoAnnotations.java:81) at org.mockito.internal.framework.DefaultMockitoSession.(DefaultMockitoSession.java:43)
at org.mockito.internal.session.DefaultMockitoSessionBuilder.startMocking(DefaultMockitoSessionBuilder.java:83)
... 54 more
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 @71c7db30
at java.base/jdk.internal.reflect.Reflection.newIllegalAccessException(Reflection.java:394)
at java.base/java.lang.reflect.AccessibleObject.checkAccess(AccessibleObject.java:674)
at java.base/java.lang.reflect.Method.invoke(Method.java:568)
at net.bytebuddy.description.annotation.AnnotationDescription$ForLoadedAnnotation.getValue(AnnotationDescription.java:803)
... 83 more
Process finished with exit code -1