J'ai activé mockings en ligne afin que je puisse me moquer des classes finales, mais maintenant j'obtiens l'exception suivante lors de l'exécution de tests avec Junit.
Lorsque j'exécute tous les tests en même temps, seuls deux tests échouent, mais si j'exécute des tests individuellement, la plupart d'entre eux échouent.
Je n'utilise aucun scala ou Android (que je connaisse). J'utilise lombok mais j'ai essayé de supprimer les annotations lombok) ne résout pas le problème.
Lors de la suppression de Mock-Maker-Inline, le problème disparaît, mais je préfère le conserver.
Comment dois-je résoudre ce problème?
org.mockito.exceptions.base.MockitoException:
Mockito cannot mock this class: class com.example.MyClass.
If you're not sure why you're getting this error, please report to the mailing list.
Java : 1.8
JVM vendor name : Oracle Corporation
JVM vendor version : 25.112-b15
JVM name : Java HotSpot(TM) 64-Bit Server VM
JVM version : 1.8.0_112-b15
JVM info : mixed mode
OS name : Windows 10
OS version : 10.0
You are seeing this disclaimer because Mockito is configured to create inlined mocks.
You can learn about inline mocks and their limitations under item #39 of the Mockito class javadoc.
Underlying exception : org.mockito.exceptions.base.MockitoException: Could not modify all classes [interface Java.lang.AutoCloseable, class com.example.MyClass, interface Java.io.Closeable, class Java.lang.Object]
at org.mockito.internal.junit.JUnitRule$1.evaluate(JUnitRule.Java:42)
at org.junit.rules.ExpectedException$ExpectedExceptionStatement.evaluate(ExpectedException.Java:239)
at org.junit.rules.RunRules.evaluate(RunRules.Java:20)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.Java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.Java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.Java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.Java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.Java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.Java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.Java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.Java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.Java:363)
at org.Eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.Java:86)
at org.Eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.Java:38)
at org.Eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.Java:539)
at org.Eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.Java:761)
at org.Eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.Java:461)
at org.Eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.Java:207)
Caused by: org.mockito.exceptions.base.MockitoException: Could not modify all classes [interface Java.lang.AutoCloseable, class com.example.MyClass, interface Java.io.Closeable, class Java.lang.Object]
at net.bytebuddy.TypeCache.findOrInsert(TypeCache.Java:138)
at net.bytebuddy.TypeCache$WithInlineExpunction.findOrInsert(TypeCache.Java:346)
at net.bytebuddy.TypeCache.findOrInsert(TypeCache.Java:161)
at net.bytebuddy.TypeCache$WithInlineExpunction.findOrInsert(TypeCache.Java:355)
... 18 more
Caused by: Java.lang.IllegalStateException:
Byte Buddy could not instrument all classes within the mock's type hierarchy
This problem should never occur for javac-compiled classes. This problem has been observed for classes that are:
- Compiled by older versions of scalac
- Classes that are part of the Android distribution
at org.mockito.internal.creation.bytebuddy.InlineBytecodeGenerator.triggerRetransformation(InlineBytecodeGenerator.Java:120)
at org.mockito.internal.creation.bytebuddy.InlineBytecodeGenerator.mockClass(InlineBytecodeGenerator.Java:97)
at org.mockito.internal.creation.bytebuddy.TypeCachingBytecodeGenerator$1.call(TypeCachingBytecodeGenerator.Java:37)
at org.mockito.internal.creation.bytebuddy.TypeCachingBytecodeGenerator$1.call(TypeCachingBytecodeGenerator.Java:34)
at net.bytebuddy.TypeCache.findOrInsert(TypeCache.Java:138)
at net.bytebuddy.TypeCache$WithInlineExpunction.findOrInsert(TypeCache.Java:346)
at net.bytebuddy.TypeCache.findOrInsert(TypeCache.Java:161)
at net.bytebuddy.TypeCache$WithInlineExpunction.findOrInsert(TypeCache.Java:355)
at org.mockito.internal.creation.bytebuddy.TypeCachingBytecodeGenerator.mockClass(TypeCachingBytecodeGenerator.Java:32)
at org.mockito.internal.creation.bytebuddy.InlineByteBuddyMockMaker.createMockType(InlineByteBuddyMockMaker.Java:200)
at org.mockito.internal.creation.bytebuddy.InlineByteBuddyMockMaker.createMock(InlineByteBuddyMockMaker.Java:181)
at org.mockito.internal.util.MockUtil.createMock(MockUtil.Java:35)
at org.mockito.internal.MockitoCore.mock(MockitoCore.Java:65)
at org.mockito.Mockito.mock(Mockito.Java:1821)
at org.mockito.internal.configuration.MockAnnotationProcessor.process(MockAnnotationProcessor.Java:36)
at org.mockito.internal.configuration.MockAnnotationProcessor.process(MockAnnotationProcessor.Java:16)
at org.mockito.internal.configuration.IndependentAnnotationEngine.createMockFor(IndependentAnnotationEngine.Java:38)
at org.mockito.internal.configuration.IndependentAnnotationEngine.process(IndependentAnnotationEngine.Java:62)
at org.mockito.internal.configuration.InjectingAnnotationEngine.processIndependentAnnotations(InjectingAnnotationEngine.Java:57)
at org.mockito.internal.configuration.InjectingAnnotationEngine.process(InjectingAnnotationEngine.Java:41)
at org.mockito.MockitoAnnotations.initMocks(MockitoAnnotations.Java:69)
... 18 more
Caused by: Java.lang.IllegalStateException: Error invoking Java.lang.reflect.Executable#getParameters
at net.bytebuddy.description.method.ParameterDescription$ForLoadedParameter$Dispatcher$ForJava8CapableVm.getParameter(ParameterDescription.Java:387)
at net.bytebuddy.description.method.ParameterDescription$ForLoadedParameter$Dispatcher$ForJava8CapableVm.isNamePresent(ParameterDescription.Java:355)
at net.bytebuddy.description.method.ParameterDescription$ForLoadedParameter.isNamed(ParameterDescription.Java:227)
at net.bytebuddy.description.method.ParameterDescription$AbstractBase.asToken(ParameterDescription.Java:147)
at net.bytebuddy.description.method.ParameterDescription$AbstractBase.asToken(ParameterDescription.Java:107)
at net.bytebuddy.description.method.ParameterList$AbstractBase.asTokenList(ParameterList.Java:80)
at net.bytebuddy.description.method.MethodDescription$AbstractBase.asToken(MethodDescription.Java:693)
at net.bytebuddy.description.method.MethodDescription$AbstractBase.asToken(MethodDescription.Java:334)
at net.bytebuddy.description.method.MethodList$AbstractBase.asTokenList(MethodList.Java:53)
at net.bytebuddy.dynamic.scaffold.InstrumentedType$Factory$Default$1.represent(InstrumentedType.Java:223)
at net.bytebuddy.ByteBuddy.redefine(ByteBuddy.Java:698)
at net.bytebuddy.ByteBuddy.redefine(ByteBuddy.Java:676)
at org.mockito.internal.creation.bytebuddy.InlineBytecodeGenerator.transform(InlineBytecodeGenerator.Java:167)
at Sun.instrument.TransformerManager.transform(TransformerManager.Java:188)
at Sun.instrument.InstrumentationImpl.transform(InstrumentationImpl.Java:428)
at Sun.instrument.InstrumentationImpl.retransformClasses0(Native Method)
at Sun.instrument.InstrumentationImpl.retransformClasses(InstrumentationImpl.Java:144)
at org.mockito.internal.creation.bytebuddy.InlineBytecodeGenerator.triggerRetransformation(InlineBytecodeGenerator.Java:117)
... 38 more
Caused by: Java.lang.reflect.MalformedParametersException: Invalid parameter name ""
at Java.lang.reflect.Executable.verifyParameters(Executable.Java:386)
at Java.lang.reflect.Executable.privateGetParameters(Executable.Java:416)
at Java.lang.reflect.Executable.getParameters(Executable.Java:357)
at Sun.reflect.GeneratedMethodAccessor2.invoke(Unknown Source)
at Sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.Java:43)
at Java.lang.reflect.Method.invoke(Method.Java:498)
at net.bytebuddy.description.method.ParameterDescription$ForLoadedParameter$Dispatcher$ForJava8CapableVm.getParameter(ParameterDescription.Java:383)
... 55 more
Utilisation de mockito-core 2.13.0 et byte-buddy 1.7.9.
Essayez d'utiliser la dernière version de ByteBuddy:
<dependency>
<groupId>net.bytebuddy</groupId>
<artifactId>byte-buddy</artifactId>
<version>1.8.11</version>
</dependency>
J'ai essayé ceci avec Mockito Inline 2.15.0
et il a résolu le même problème avec ma demande. Il semble que ce soit un problème connu et qu'il ait été résolu dans la dernière version de ByteBuddy.
J'ai contourné cela en passant à la dernière version de mockito. Actuellement c'est:
testImplementation "org.mockito:mockito-core:2.25.1"
Après cela, j'ai eu quelques erreurs de construction qui ont été résolues en sélectionnant
File->Invalidate Caches / Restart...
pour moi d'ajouter
web-démarrage-à-ressort
la dépendance à mon pom fonctionnait car l'interface que j'essayais de moquer faisait partie du pot externe qui utilisait feign client.