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:
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:
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!
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