'mock-maker-inline - Could not initialize inline Byte Buddy mock maker (power-mockito + jdk 8 + bytebuddy)

I am using Powermockito to mock and spy static classes. Version: 2.0.4

Also using mockito-core version 3.0.0. In, one of my projects, i am able to mockStatic by enabling mock-maker-inline as specified here https://github.com/powermock/powermock-examples-maven/blob/master/mockito2/src/test/resources/org/powermock/extensions/configuration.properties

But, when i tried the same approach on my another project, facing the following error:

Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.ghx.tpm.api.ISubscribedProductParameterService]: Factory method 'subscribedProductParameterService' threw exception; nested exception is java.lang.IllegalStateException: Could not initialize plugin: interface org.mockito.plugins.MockMaker (alternate: null)
Caused by: java.lang.IllegalStateException: Could not initialize plugin: interface org.mockito.plugins.MockMaker (alternate: null)
Caused by: java.lang.IllegalStateException: Failed to load interface org.mockito.plugins.MockMaker implementation declared in sun.misc.CompoundEnumeration@614aeccc
Caused by: java.lang.IllegalStateException: Failed to load MockMaker implementation: mock-maker-inline
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
Caused by: org.mockito.exceptions.base.MockitoInitializationException: 

Could not initialize inline Byte Buddy mock maker. (This mock maker is not supported on Android.)

Java               : 1.8
JVM vendor name    : Oracle Corporation
JVM vendor version : 25.161-b12
JVM name           : Java HotSpot(TM) 64-Bit Server VM
JVM version        : 1.8.0_161-b12
JVM info           : mixed mode
OS name            : Linux
OS version         : 4.4.0-166-generic

Caused by: java.lang.IllegalStateException: Error during attachment using: net.bytebuddy.agent.ByteBuddyAgent$AttachmentProvider$Compound@3c89bb12
Caused by: java.lang.reflect.InvocationTargetException
Caused by: java.lang.NullPointerException 

Can you please help in resolving this issue?

Additional details:

  • I am mocking static spy class here.
  • byte-buddy jar resolved dependency version is 1.9.10
  • will overlap of mockito2 and powermock-api-mockito2 will cause issue in loading MockMaker? how to resolve them?
  • If the test is executed in IDE it is working fine. but fails when run in maven console.

Additional findings: Ensured JavassistMockClassLoader is used by adding @PrepareForTest and PowerMockRunner (since one of my other project worked with same configuration)

Facing Null pointer Exception exactly at this line:

ByteBuddyAgent.java (private static void install(AttachmentProvider attachmentProvider, String processId, String argument, AgentProvider agentProvider)
 Attacher.install(attachmentAccessor.getVirtualMachineType(), processId, agentProvider.resolve().getAbsolutePath(), argument);

Classloader while it is failing is - AppLaunchClassLoader. ( Whereas for working project it is URLClassLoader). Not sure if this difference could be a reason.

Update 2:

  • @PowerMockRule --> This Anntoation initializes JVM agent once
  • During Mockito.mock invocation, it tries to load mock-maker-inline, which inturn loads Agent into JVM. Will this cause any issue? I believe, VirtualMachine.attach() method is invoked twice. Not sure if it will cause any problem.

Eclipse IDE loads mock-maker-inline via tools.jar (working fine in IDE) Maven command line loads mock-maker-inline via powermock-module-javaagent (failing with initialization error).- Any suggestions to force maven console to pick up mock-maker-inline initialization using tools.jar?

Update 3: I went to a minimalistic approach. Removed all Spring runners, Bean creations, Context Configurations. Just kept a plain PowerMockRunner + static mock on UUID.randomUUID() + Sysout.

Still facing mock-maker-inline initialization error. feeling very wierd!! same configuration works on my other project in same workspace!



Solution 1:[1]

Found the issue. @PowerMockRule (which utilizes powermock-modules-javaagent.JAR) is the culprit.

Using @PowerMockRule along with mock-maker-inline causing this issue.

I have to Replace PowerMockRule with @RunsWith(PowerMockRunner.class) to fix this. Thanks.

Update 1: after above fix, facing issues with classloader as discussed here https://github.com/raphw/byte-buddy/issues/813. Please feel free to share your suggestions. Thanks

Solution 2:[2]

Be sure to choose a JDK, not "jre".

Look into the file Path to see if it is jdk or jre.

enter image description here

Could not initialize plugin: interface org.mockito.plugins.MockMaker

Could not initialize inline Byte Buddy mock maker. (This mock maker is not supported on Android.)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2