最終クラスをモックできるように インラインモック を有効にしましたが、Junitでテストを実行すると、次の例外が発生します。
すべてのテストを一度に実行すると、2つのテストのみが失敗しますが、個別にテストを実行すると、ほとんどのテストが失敗します。
scalaまたはAndroidクラス(私が知っていること)を使用していません。)ロンボクを使用していますが、ロンボクの注釈を削除しようとしました問題を修正しないでください。
Mock-maker-inlineを削除すると問題は解消されますが、私はそれを維持したいと思います。
この問題にどのように対処すればよいですか?
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
Mockito-core 2.13.0およびbyte-buddy 1.7.9を使用します。
ByteBuddyの最新バージョンを使用してみてください。
<dependency>
<groupId>net.bytebuddy</groupId>
<artifactId>byte-buddy</artifactId>
<version>1.8.11</version>
</dependency>
私はMockito Inlineでこれを試しました2.15.0
と私のアプリケーションで同じ問題を解決しました。これは既知の問題であるように見え、ByteBuddyの最新バージョンで解決されています。
Mockitoを最新バージョンにアップグレードすることで、これを回避しました。現在は次のとおりです。
testImplementation "org.mockito:mockito-core:2.25.1"
その後、いくつかのビルドエラーが発生しましたが、これを選択することで解決しました
File->Invalidate Caches / Restart...
私にとっては
春ブートスターターウェブ
私が模倣しようとしていたインターフェイスは、偽のクライアントを使用する外部jarの一部だったので、私のpomへの依存関係は機能しました。