Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to enable final class mocking with mockito in eclipse project?

Tags:

mockito

I am using mockito-core-2.7.21 in a maven project and it is working as expected.

But to enable mocking of final class I created a file mockito-extensions\org.mockito.plugins.MockMaker under my source folder and added the text mock-maker-inline.

As per https://www.baeldung.com/mockito-final and also Mock final class with Mockito 2, it should allow me to mock final classes but when i running my junit test I am getting the below error:

Java Version: 1.8

OS: Windows 10

Junit: 4 and 5(I tried both)

java.lang.IllegalStateException: Could not initialize plugin: interface org.mockito.plugins.MockMaker
    at org.mockito.internal.configuration.plugins.PluginLoader$1.invoke(PluginLoader.java:66)
    at com.sun.proxy.$Proxy7.isTypeMockable(Unknown Source)
    at org.mockito.internal.util.MockUtil.typeMockabilityOf(MockUtil.java:29)
    at org.mockito.internal.util.MockCreationValidator.validateType(MockCreationValidator.java:22)
    at org.mockito.internal.creation.MockSettingsImpl.validatedSettings(MockSettingsImpl.java:186)
    at org.mockito.internal.creation.MockSettingsImpl.confirm(MockSettingsImpl.java:180)
    at org.mockito.internal.MockitoCore.mock(MockitoCore.java:62)
    at org.mockito.Mockito.mock(Mockito.java:1729)
    at org.mockito.Mockito.mock(Mockito.java:1642)
    at siemens.esd.grpc.services.UTCreateModuleServiceTest.testCreateBlockInterface(UTCreateModuleServiceTest.java:29)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
    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.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:89)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:41)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:541)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:763)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:463)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:209)
Caused by: java.lang.IllegalStateException: Failed to load interface org.mockito.plugins.MockMaker implementation declared in sun.misc.CompoundEnumeration@6f75e721
    at org.mockito.internal.configuration.plugins.PluginLoader.loadImpl(PluginLoader.java:101)
    at org.mockito.internal.configuration.plugins.PluginLoader.loadPlugin(PluginLoader.java:45)
    at org.mockito.internal.configuration.plugins.PluginRegistry.<init>(PluginRegistry.java:18)
    at org.mockito.internal.configuration.plugins.Plugins.<clinit>(Plugins.java:17)
    at org.mockito.internal.configuration.GlobalConfiguration.tryGetPluginAnnotationEngine(GlobalConfiguration.java:55)
    at org.mockito.MockitoAnnotations.initMocks(MockitoAnnotations.java:68)
    at siemens.esd.grpc.services.UTCreateModuleServiceTest.setUp(UTCreateModuleServiceTest.java:22)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:24)
    ... 15 more
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.181-b13
JVM name           : Java HotSpot(TM) 64-Bit Server VM
JVM version        : 1.8.0_181-b13
JVM info           : mixed mode
OS name            : Windows 10
OS version         : 10.0

    at org.mockito.internal.creation.bytebuddy.InlineByteBuddyMockMaker.<init>(InlineByteBuddyMockMaker.java:172)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
    at java.lang.reflect.Constructor.newInstance(Unknown Source)
    at java.lang.Class.newInstance(Unknown Source)
    at org.mockito.internal.configuration.plugins.PluginLoader.loadImpl(PluginLoader.java:96)
    ... 29 more
Caused by: java.lang.IllegalStateException: No compatible attachment provider is not available
    at net.bytebuddy.agent.ByteBuddyAgent.install(ByteBuddyAgent.java:347)
    at net.bytebuddy.agent.ByteBuddyAgent.install(ByteBuddyAgent.java:332)
    at net.bytebuddy.agent.ByteBuddyAgent.install(ByteBuddyAgent.java:300)
    at net.bytebuddy.agent.ByteBuddyAgent.install(ByteBuddyAgent.java:286)
    at org.mockito.internal.creation.bytebuddy.InlineByteBuddyMockMaker.<clinit>(InlineByteBuddyMockMaker.java:102)
    ... 35 more
like image 725
Sanjit Kumar Mishra Avatar asked Mar 06 '23 00:03

Sanjit Kumar Mishra


1 Answers

After struggling I found the problem is with the java library available in your java build path. If you have JRE library in you java build path instead of JDK library then bytebuddy utility is throwing this issue.

Hence the solution to the problem is use JDK library instead of JRE library.

like image 60
Sanjit Kumar Mishra Avatar answered Apr 09 '23 13:04

Sanjit Kumar Mishra