Skip to content

Boot v2.6.x breaks Mockito from v4.5.x #30793

@runeflobakk

Description

@runeflobakk

Using Mockito v4.5.0 (and 4.5.1) together with spring-boot-dependencies v2.6.7 throws this exception:

It seems like you are running Mockito with an incomplete or inconsistent class path. Byte Buddy could not be loaded.
...
Caused by: java.lang.ClassNotFoundException: net.bytebuddy.utility.GraalImageCode
	at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:641)
	at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:188)
	at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:520)
	... 133 common frames omitted
Complete stacktrace
java.lang.IllegalStateException: Could not initialize plugin: interface org.mockito.plugins.MockMaker (alternate: null)
	at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:185)
	at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:653)
	... 105 common frames omitted
Caused by: java.lang.IllegalStateException: Could not initialize plugin: interface org.mockito.plugins.MockMaker (alternate: null)
	at org.mockito.internal.configuration.plugins.PluginLoader$1.invoke(PluginLoader.java:88)
	at jdk.proxy2/jdk.proxy2.$Proxy122.isTypeMockable(Unknown Source)
	at org.mockito.internal.util.MockUtil.typeMockabilityOf(MockUtil.java:33)
	at org.mockito.internal.util.MockCreationValidator.validateType(MockCreationValidator.java:22)
	at org.mockito.internal.creation.MockSettingsImpl.validatedSettings(MockSettingsImpl.java:250)
	at org.mockito.internal.creation.MockSettingsImpl.build(MockSettingsImpl.java:232)
	at org.mockito.internal.MockitoCore.mock(MockitoCore.java:94)
	at org.mockito.Mockito.mock(Mockito.java:1965)
	at 
	at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:244)
	at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:331)
        at 
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77)
	at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.base/java.lang.reflect.Method.invoke(Method.java:568)
	at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:154)
	... 106 common frames omitted
Caused by: java.lang.IllegalStateException: Internal problem occurred, please report it. Mockito is unable to load the default implementation of class that is a part of Mockito distribution. Failed to load interface org.mockito.plugins.MockMaker
	at org.mockito.internal.configuration.plugins.DefaultMockitoPlugins.create(DefaultMockitoPlugins.java:85)
	at org.mockito.internal.configuration.plugins.DefaultMockitoPlugins.getDefaultPlugin(DefaultMockitoPlugins.java:59)
	at org.mockito.internal.configuration.plugins.PluginLoader.loadPlugin(PluginLoader.java:79)
	at org.mockito.internal.configuration.plugins.PluginLoader.loadPlugin(PluginLoader.java:54)
	at org.mockito.internal.configuration.plugins.PluginRegistry.(PluginRegistry.java:28)
	at org.mockito.internal.configuration.plugins.Plugins.(Plugins.java:22)
	at org.mockito.internal.MockitoCore.(MockitoCore.java:77)
	at org.mockito.Mockito.(Mockito.java:1614)
	... 117 common frames omitted
Caused by: java.lang.reflect.InvocationTargetException: null
	at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
	at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:77)
	at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
	at java.base/java.lang.reflect.Constructor.newInstanceWithCaller(Constructor.java:499)
	at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:480)
	at org.mockito.internal.configuration.plugins.DefaultMockitoPlugins.create(DefaultMockitoPlugins.java:83)
	... 124 common frames omitted
Caused by: org.mockito.exceptions.base.MockitoInitializationException: 
It seems like you are running Mockito with an incomplete or inconsistent class path. Byte Buddy could not be loaded.

Byte Buddy is available on Maven Central as 'net.bytebuddy:byte-buddy' with the module name 'net.bytebuddy'.
For the inline mock maker, 'net.bytebuddy:byte-buddy-agent' with the module name 'net.bytebuddy.agent' is also required.
Normally, your IDE or build tool (such as Maven or Gradle) should take care of your class path completion but
at org.mockito.internal.exceptions.Reporter.missingByteBuddyDependency(Reporter.java:1127)
at org.mockito.internal.creation.bytebuddy.ByteBuddyMockMaker.(ByteBuddyMockMaker.java:31)
... 130 common frames omitted
Caused by: java.lang.NoClassDefFoundError: net/bytebuddy/utility/GraalImageCode
at org.mockito.internal.creation.bytebuddy.SubclassInjectionLoader.(SubclassInjectionLoader.java:34)
at org.mockito.internal.creation.bytebuddy.SubclassByteBuddyMockMaker.(SubclassByteBuddyMockMaker.java:33)
at org.mockito.internal.creation.bytebuddy.ByteBuddyMockMaker.(ByteBuddyMockMaker.java:29)
... 130 common frames omitted
Caused by: java.lang.ClassNotFoundException: net.bytebuddy.utility.GraalImageCode
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:641)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:188)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:520)
... 133 common frames omitted

As I am not sure if this should be solved in Spring Boot (I am not familiar with policies for updating these libs, as I assume Byte Buddy is extensively used by the Spring framework). I am registering the issue for reference.

Workaround

I like to keep up with Mockito for my tests, so a workaround if you are affected by this is to include this in your dependencyManagement (if you don't mind using a more recent minor-version of Byte Buddy than what is specified in spring-boot-dependencies):

    ...
    </dependencyManagement>
        </dependencies>
            ...
            <dependency>
                <groupId>net.bytebuddy</groupId>
                <artifactId>byte-buddy</artifactId>
                <version>1.12.9</version>
            </dependency>
            <dependency>
                <groupId>net.bytebuddy</groupId>
                <artifactId>byte-buddy-agent</artifactId>
                <version>1.12.9</version>
            </dependency>
            ...

Though this is not ideal, because you now must (should) manually keep the Byte Buddy version in sync with your Mockito version.

References

Spring Boot 2.6.7 specifies:

Metadata

Metadata

Assignees

No one assigned

    Labels

    status: declinedA suggestion or change that we don't feel we should currently apply

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions