Mockmaker exception - Could not initialize inline Byte Buddy mock maker

1.6k Views Asked by At

My project uses the below mockito dependencies. I am facing the below issue only when I added my company's internal dependency. How to fix it? I already tried making sure my Intellij dea is using JDK not JRE as suggested by other answers.

<dependency>

    <groupId>org.mockito</groupId>
    <artifactId>mockito-inline</artifactId>
    <version>3.12.4</version>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>org.mockito</groupId>
    <artifactId>mockito-junit-jupiter</artifactId>
    <scope>test</scope>
</dependency>
Caused by: org.mockito.exceptions.base.MockitoInitializationException: 
    Could not initialize inline Byte Buddy mock maker.
    It appears as if your JDK does not supply a working agent attachment mechanism.
    Java               : 1.8
    JVM vendor name    : Azul Systems, Inc.
    JVM vendor version : 25.232-b18
    JVM name           : OpenJDK 64-Bit Server VM
    JVM version        : 1.8.0_232-b18
    JVM info           : mixed mode
    OS name            : Mac OS X
    OS version         : 10.16
        at org.mockito.internal.creation.bytebuddy.InlineDelegateByteBuddyMockMaker.<init>(InlineDelegateByteBuddyMockMaker.java:246)
        at org.mockito.internal.creation.bytebuddy.InlineByteBuddyMockMaker.<init>(InlineByteBuddyMockMaker.java:25)
        ... 73 more
    Caused by: java.lang.IllegalStateException: Error during attachment using: net.bytebuddy.agent.ByteBuddyAgent$AttachmentProvider$Compound@d70f722
        at net.bytebuddy.agent.ByteBuddyAgent.install(ByteBuddyAgent.java:638)
        at net.bytebuddy.agent.ByteBuddyAgent.install(ByteBuddyAgent.java:611)
        at net.bytebuddy.agent.ByteBuddyAgent.install(ByteBuddyAgent.java:563)
        at net.bytebuddy.agent.ByteBuddyAgent.install(ByteBuddyAgent.java:540)
        at org.mockito.internal.creation.bytebuddy.InlineDelegateByteBuddyMockMaker.<clinit>(InlineDelegateByteBuddyMockMaker.java:117)
        ... 74 more
    Caused by: java.lang.reflect.InvocationTargetException
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:498)
        at net.bytebuddy.agent.Attacher.install(Attacher.java:102)
        at net.bytebuddy.agent.ByteBuddyAgent.install(ByteBuddyAgent.java:633)
        ... 78 more
    Caused by: com.sun.tools.attach.AttachNotSupportedException: no providers installed
        at com.sun.tools.attach.VirtualMachine.attach(VirtualMachine.java:208)
        ... 84 more
    [ERROR] testGenerateConnectLiteUrlServerError  Time elapsed: 0 s  <<< ERROR!
    java.lang.IllegalStateException: Could not initialize plugin: interface org.mockito.plugins.MockMaker (alternate: null)
3

There are 3 best solutions below

2
Rafael Winterhalter On

As the exception suggests: your VM does not support an attach mechanism. You can add JNA as a test dependency to allow Byte Buddy to emulate it.

0
Robin Bruneel On

You'll have to activate your jdk attachment settings. This will only be applied in your test step: https://docs.gradle.org/current/userguide/build_lifecycle.html

For Gradle

test {
    jvmArgs '-Djdk.attach.allowAttachSelf=true'
}

For Maven with Surefire:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
     <artifactId>maven-surefire-plugin</artifactId>
     <version>3.1.2</version>
     <configuration>
       <argLine>-Djdk.attach.allowAttachSelf=true </argLine>
     </configuration>
</plugin>
0
Deividson Calixto On

May be you need add correct dependencies for byte budy.

Follow this steps:

  1. First: look at this maven repository for you version, in this case: https://mvnrepository.com/artifact/org.mockito/mockito-inline/3.12.4

  2. At the site you will see this dependencies are needed for mockito-inline-3.12.4: enter image description here

  3. Click on link and you will see the dependencies used for this one mockito-core, like bellow:

enter image description here

  1. Add all dependencies in that picture above to your pox.xml, it'll apear like this:

         <dependency>
         <groupId>org.mockito</groupId>
         <artifactId>mockito-inline</artifactId>
         <version>4.11.0</version>
         <scope>test</scope>
     </dependency>
    
     <dependency>
         <groupId>org.mockito</groupId>
         <artifactId>mockito-junit-jupiter</artifactId>
         <version>4.11.0</version>
         <scope>test</scope>
     </dependency>
    
     <!-- https://mvnrepository.com/artifact/net.bytebuddy/byte-buddy -->
     <dependency>
         <groupId>net.bytebuddy</groupId>
         <artifactId>byte-buddy</artifactId>
         <version>1.12.19</version>
     </dependency>
    
     <!-- https://mvnrepository.com/artifact/net.bytebuddy/byte-buddy-agent -->
     <dependency>
         <groupId>net.bytebuddy</groupId>
         <artifactId>byte-buddy-agent</artifactId>
         <version>1.12.19</version>
         <scope>test</scope>
     </dependency>
    
  2. try a command on terminal: mvn clean test

I hope this can help, worked for me and i found this tip at: https://howtodoinjava.com/mockito/plugin-mockmaker-error/

Related Questions in ILLEGALSTATEEXCEPTION