Jacoco + Mockito:NoClassDefFoundError:com.foo.Foo$MockitoMock$yT0N0ogW(初始化失败)

e1xvtsh3  于 2023-08-05  发布在  其他
关注(0)|答案(1)|浏览(149)

我有一个使用jacoco-maven-plugin + maven-surefire-plugin + Mockito的项目:
配置没有什么特别的:

<plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>org.jacoco</groupId>
                <artifactId>jacoco-maven-plugin</artifactId>
                <version>${jacoco-maven-plugin.version}</version>
                <executions>
                    <execution>
                        <id>prepare-agent</id>
                        <goals>
                            <goal>prepare-agent</goal>
                        </goals>
                    </execution>
                    <execution>
                        <id>report</id>
                        <phase>prepare-package</phase>
                        <goals>
                            <goal>report</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>

字符串
但我得到了这个错误:

java.lang.NoClassDefFoundError: com.foo.Foo$MockitoMock$yT0N0ogW (initialization failure)
        at java.base/java.lang.J9VMInternals.initializationAlreadyFailed(J9VMInternals.java:156) ~[na:na]
        at java.base/jdk.internal.misc.Unsafe.allocateInstance(Native Method) ~[na:na]
        at jdk.unsupported/sun.misc.Unsafe.allocateInstance(Unsafe.java:864) ~[na:na]
        at org.objenesis.instantiator.sun.UnsafeFactoryInstantiator.newInstance(UnsafeFactoryInstantiator.java:47) ~[objenesis-3.3.jar:3.3]
        at org.objenesis.ObjenesisBase.newInstance(ObjenesisBase.java:73) ~[objenesis-3.3.jar:3.3]
        at org.mockito.internal.creation.instance.ObjenesisInstantiator.newInstance(ObjenesisInstantiator.java:22) ~[mockito-core-5.3.1.jar:na]
        at org.mockito.internal.creation.bytebuddy.InlineDelegateByteBuddyMockMaker.doCreateMock(InlineDelegateByteBuddyMockMaker.java:376) ~[mockito-core-5.3.1.jar:na]
        at org.mockito.internal.creation.bytebuddy.InlineDelegateByteBuddyMockMaker.createMock(InlineDelegateByteBuddyMockMaker.java:334) ~[mockito-core-5.3.1.jar:na]
        at org.mockito.internal.creation.bytebuddy.InlineByteBuddyMockMaker.createMock(InlineByteBuddyMockMaker.java:56) ~[mockito-core-5.3.1.jar:na]
        at org.mockito.internal.util.MockUtil.createMock(MockUtil.java:99) ~[mockito-core-5.3.1.jar:na]
        at org.mockito.internal.MockitoCore.mock(MockitoCore.java:88) ~[mockito-core-5.3.1.jar:na]
        at org.mockito.Mockito.mock(Mockito.java:2101) ~[mockito-core-5.3.1.jar:na]
        at org.springframework.boot.test.mock.mockito.SpyDefinition.createSpy(SpyDefinition.java:116) ~[spring-boot-test-3.1.1.jar:3.1.1]
        at org.springframework.boot.test.mock.mockito.MockitoPostProcessor.createSpyIfNecessary(MockitoPostProcessor.java:331) ~[spring-boot-test-3.1.1.jar:3.1.1]
        at org.springframework.boot.test.mock.mockito.MockitoPostProcessor$SpyPostProcessor.postProcessAfterInitialization(MockitoPostProcessor.java:465) ~[spring-boot-test-3.1.1.jar:3.1.1]
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsAfterInitialization(AbstractAutowireCapableBeanFactory.java:434) ~[spring-beans-6.0.10.jar:6.0.10]
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.postProcessObjectFromFactoryBean(AbstractAutowireCapableBeanFactory.java:1885) ~[spring-beans-6.0.10.jar:6.0.10]
        at org.springframework.beans.factory.support.FactoryBeanRegistrySupport.getObjectFromFactoryBean(FactoryBeanRegistrySupport.java:105) ~[spring-beans-6.0.10.jar:6.0.10]
        ... 121 common frames omitted
Caused by: java.lang.NullPointerException: null
        at java.base/java.util.ArrayList.add(ArrayList.java:453) ~[na:na]
        at java.base/java.util.ArrayList.add(ArrayList.java:467) ~[na:na]
        at java.base/java.lang.invoke.MethodTypeHelper.parseIntoClass(MethodTypeHelper.java:237) ~[na:na]
        at java.base/java.lang.invoke.MethodHandleResolver.fromFieldDescriptorString(MethodHandleResolver.java:306) ~[na:na]
        at java.base/java.lang.invoke.MethodHandleResolver.resolveConstantDynamic(MethodHandleResolver.java:102) ~[na:na]
        at com.foo.Foo$MockitoMock$yT0N0ogW.<clinit>(Unknown Source) ~[classes/:na]
        at java.base/jdk.internal.misc.Unsafe.allocateInstance(Native Method) ~[na:na]
        at java.base/java.lang.invoke.DirectMethodHandle.allocateInstance(DirectMethodHandle.java:520) ~[na:na]
        at java.base/java.lang.invoke.MethodHandle.invokeWithArguments(MethodHandle.java:732) ~[na:na]
        at org.mockito.internal.util.reflection.InstrumentationMemberAccessor$Dispatcher$ByteBuddy$vQ02mWmG.invokeWithArguments(Unknown Source) ~[na:na]
        at org.mockito.internal.util.reflection.InstrumentationMemberAccessor.lambda$newInstance$0(InstrumentationMemberAccessor.java:199) ~[mockito-core-5.3.1.jar:na]
        at org.mockito.internal.creation.bytebuddy.InlineDelegateByteBuddyMockMaker.lambda$newInstance$4(InlineDelegateByteBuddyMockMaker.java:636) ~[mockito-core-5.3.1.jar:na]
        at org.mockito.internal.util.reflection.InstrumentationMemberAccessor.newInstance(InstrumentationMemberAccessor.java:188) ~[mockito-core-5.3.1.jar:na]
        at org.mockito.internal.util.reflection.ModuleMemberAccessor.newInstance(ModuleMemberAccessor.java:49) ~[mockito-core-5.3.1.jar:na]
        at org.mockito.internal.creation.bytebuddy.InlineDelegateByteBuddyMockMaker.newInstance(InlineDelegateByteBuddyMockMaker.java:631) ~[mockito-core-5.3.1.jar:na]
        at org.mockito.internal.creation.bytebuddy.InlineDelegateByteBuddyMockMaker.doCreateMock(InlineDelegateByteBuddyMockMaker.java:369) ~[mockito-core-5.3.1.jar:na]
        ... 132 common frames omitted


<argLine>...</argLine>添加到surefire配置中可以使其运行,但无法生成jacoco.exec,也无法生成报告文件。
有谁知道为什么会发生这个错误,什么可以使测试过程运行良好,并生成jacoco报告?

eh57zj3b

eh57zj3b1#

我自己找到了答案,因为我经历了无数挣扎的日子。
是关于mockito-inline:5.2.0的,我用mockito-subclass:5.3.1+替换了它。在某些情况下,您也可以同时使用两者。
顺便说一句,他们不会有mockito-inline:5.3.0+,但mockito-subclass有。

相关问题