Saya menggunakan Mockito 2.23.4 dan Byte-Buddy 1.9.3 dalam proyek saya. Saya ingin mengejek kelas akhir dan metode, jadi saya menggunakan Mockito-inline mengikuti posting ini Mockito Mock kelas dan metode dan pengaturan mock-maker-inline di dalam org.mockito.plugins.MockMaker File di direktori src/test/resources/mockito-extensions proyek. Saya juga menindaklanjuti posting Mockito gagal dengan inline Mocks Diaktifkan dengan pengecualian nama parameter yang tidak valid tetapi masalah masih ada di sana. Tetapi ketika saya menjalankan tes saya mendapatkan jejak tumpukan di bawah ini.

Java HotSpot(TM) 64-Bit Server VM warning: Sharing is only supported for boot loader classes because bootstrap classpath has been appended

Test ignored.

org.mockito.exceptions.base.MockitoException: 
Mockito cannot mock this class: class org.elasticsearch.client.RestHighLevelClient.

If you're not sure why you're getting this error, please report to the mailing list.


Java               : 15
JVM vendor name    : Oracle Corporation
JVM vendor version : 15.0.2+7-27
JVM name           : Java HotSpot(TM) 64-Bit Server VM
JVM version        : 15.0.2+7-27
JVM info           : mixed mode, sharing
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 [class org.elasticsearch.client.RestHighLevelClient, class java.lang.Object, interface java.io.Closeable, interface java.lang.AutoCloseable]

    at org.mockito.internal.runners.DefaultInternalRunner$1.withBefores(DefaultInternalRunner.java:39)
    at org.junit.runners.BlockJUnit4ClassRunner.methodBlock(BlockJUnit4ClassRunner.java:276)
    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.mockito.internal.runners.DefaultInternalRunner$1.run(DefaultInternalRunner.java:79)
    at org.mockito.internal.runners.DefaultInternalRunner.run(DefaultInternalRunner.java:85)
    at org.mockito.internal.runners.StrictRunner.run(StrictRunner.java:39)
    at org.mockito.junit.MockitoJUnitRunner.run(MockitoJUnitRunner.java:163)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:69)
    at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:33)
    at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:220)
    at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:53)
Caused by: org.mockito.exceptions.base.MockitoException: Could not modify all classes [class org.elasticsearch.client.RestHighLevelClient, class java.lang.Object, interface java.io.Closeable, interface java.lang.AutoCloseable]
    at net.bytebuddy.TypeCache.findOrInsert(TypeCache.java:152)
    at net.bytebuddy.TypeCache$WithInlineExpunction.findOrInsert(TypeCache.java:365)
    at net.bytebuddy.TypeCache.findOrInsert(TypeCache.java:174)
    at net.bytebuddy.TypeCache$WithInlineExpunction.findOrInsert(TypeCache.java:376)
    ... 19 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:164)
    at org.mockito.internal.creation.bytebuddy.InlineBytecodeGenerator.mockClass(InlineBytecodeGenerator.java:141)
    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:152)
    at net.bytebuddy.TypeCache$WithInlineExpunction.findOrInsert(TypeCache.java:365)
    at net.bytebuddy.TypeCache.findOrInsert(TypeCache.java:174)
    at net.bytebuddy.TypeCache$WithInlineExpunction.findOrInsert(TypeCache.java:376)
    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:69)
    at org.mockito.Mockito.mock(Mockito.java:1896)
    at org.mockito.internal.configuration.MockAnnotationProcessor.processAnnotationForMock(MockAnnotationProcessor.java:44)
    at org.mockito.internal.configuration.MockAnnotationProcessor.process(MockAnnotationProcessor.java:19)
    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)
    ... 19 more
Caused by: java.lang.IllegalArgumentException: Unsupported class file major version 59
    at net.bytebuddy.jar.asm.ClassReader.<init>(ClassReader.java:184)
    at net.bytebuddy.jar.asm.ClassReader.<init>(ClassReader.java:166)
    at net.bytebuddy.jar.asm.ClassReader.<init>(ClassReader.java:152)
    at net.bytebuddy.utility.OpenedClassReader.of(OpenedClassReader.java:82)
    at net.bytebuddy.dynamic.scaffold.TypeWriter$Default$ForInlining.create(TypeWriter.java:3390)
    at net.bytebuddy.dynamic.scaffold.TypeWriter$Default.make(TypeWriter.java:1930)
    at net.bytebuddy.dynamic.scaffold.inline.RedefinitionDynamicTypeBuilder.make(RedefinitionDynamicTypeBuilder.java:207)
    at net.bytebuddy.dynamic.scaffold.inline.AbstractInliningDynamicTypeBuilder.make(AbstractInliningDynamicTypeBuilder.java:116)
    at net.bytebuddy.dynamic.DynamicType$Builder$AbstractBase.make(DynamicType.java:3394)
    at org.mockito.internal.creation.bytebuddy.InlineBytecodeGenerator.transform(InlineBytecodeGenerator.java:215)
    at java.instrument/java.lang.instrument.ClassFileTransformer.transform(ClassFileTransformer.java:246)
    at java.instrument/sun.instrument.TransformerManager.transform(TransformerManager.java:188)
    at java.instrument/sun.instrument.InstrumentationImpl.transform(InstrumentationImpl.java:563)
    at java.instrument/sun.instrument.InstrumentationImpl.retransformClasses0(Native Method)
    at java.instrument/sun.instrument.InstrumentationImpl.retransformClasses(InstrumentationImpl.java:167)
    at org.mockito.internal.creation.bytebuddy.InlineBytecodeGenerator.triggerRetransformation(InlineBytecodeGenerator.java:161)
    ... 40 more

Saya mencoba mengikuti pengaturan ketergantungan untuk byte-sobat seperti yang disebutkan dalam posting Mockito gagal dengan tiruan inline :

        <dependency>
            <groupId>net.bytebuddy</groupId>
            <artifactId>byte-buddy</artifactId>
            <version>LATEST</version>
        </dependency>

Tetapi masih tidak berhasil dalam menyelesaikan kesalahan. Kode tes dan metode aktual disebutkan dalam posting saya sebelumnya: Mendapatkan Kesalahan NPE Ketika mencoba Mock ElasticClient di Mockito [Duplikat].

Apa yang bisa saya coba selanjutnya?

1
Gregory Javis 4 April 2021, 11:53

1 menjawab

Jawaban Terbaik

Anda perlu memperbarui dependensi Anda.

Anda mengkompilasi proyek Anda dengan JDK15. ByteBuddy mengeluh tidak dapat instrumen file kelas yang dihasilkan oleh JDK ini (file kelas yang tidak didukung versi utama 59)

Dukungan untuk Java 15 diperkenalkan dalam ByteBuddy Rilis 1.10.6

Perhatikan bahwa Anda tidak perlu menentukan versi bytebuddy dalam proyek Anda karena Mockito menariknya secara transitif (kecuali YounHave alasan bagus untuk memaksakan versi).

Versi bytebuddy yang kompatibel diperkenalkan di Mockito 3.3.10. Lihat komit mengubah versi ByteBuddy ke 1.10.10

Alih-alih menganalisis sejarah komit di Mockito, saya akan merekomendasikan pergi dengan terbaru dan terhebat (3.8.0 pada saat penulisan)

2
Lesiak 4 April 2021, 22:24