question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Exception when using mock-maker-inline

See original GitHub issue

I was trying out mock-maker-inline feature with the following example:

import static org.mockito.Mockito.*;

final class FinalClass {
  int m1() {
    throw new RuntimeException("42");
  }
}

public class TestMockFinal {
  public static void main(String[] args) {
    FinalClass cl = mock(FinalClass.class);
    when(cl.m1()).thenReturn(-1);
    if (cl.m1() != -1) {
      throw new Error("Not mocked!");
    }
  }
}

When I run this code I get the following error:

Exception in thread "main" java.lang.ExceptionInInitializerError
	at org.mockito.internal.util.MockUtil.<clinit>(MockUtil.java:24)
	at org.mockito.internal.util.MockCreationValidator.validateType(MockCreationValidator.java:22)
	at org.mockito.internal.creation.MockSettingsImpl.validatedSettings(MockSettingsImpl.java:168)
	at org.mockito.internal.creation.MockSettingsImpl.confirm(MockSettingsImpl.java:162)
	at org.mockito.internal.MockitoCore.mock(MockitoCore.java:64)
	at org.mockito.Mockito.mock(Mockito.java:1665)
	at org.mockito.Mockito.mock(Mockito.java:1578)
	at TestMockFinal.main(TestMockFinal.java:11)
Caused by: java.lang.IllegalStateException: Failed to load interface org.mockito.plugins.MockMaker implementation declared in sun.misc.CompoundEnumeration@379619aa
	at org.mockito.internal.configuration.plugins.PluginLoader.loadImpl(PluginLoader.java:86)
	at org.mockito.internal.configuration.plugins.PluginLoader.loadPlugin(PluginLoader.java:40)
	at org.mockito.internal.configuration.plugins.PluginRegistry.<init>(PluginRegistry.java:18)
	at org.mockito.internal.configuration.plugins.Plugins.<clinit>(Plugins.java:17)
	... 8 more
Caused by: java.lang.IllegalStateException: Error during attachment using: ByteBuddyAgent.AttachmentProvider.Compound{attachmentProviders=[ByteBuddyAgent.AttachmentProvider.ForJigsawVm.INSTANCE, ByteBuddyAgent.AttachmentProvider.ForJ9Vm.INSTANCE, ByteBuddyAgent.AttachmentProvider.ForToolsJarVm.JVM_ROOT, ByteBuddyAgent.AttachmentProvider.ForToolsJarVm.JDK_ROOT, ByteBuddyAgent.AttachmentProvider.ForToolsJarVm.MACINTOSH]}
	at net.bytebuddy.agent.ByteBuddyAgent.install(ByteBuddyAgent.java:314)
	at net.bytebuddy.agent.ByteBuddyAgent.install(ByteBuddyAgent.java:280)
	at net.bytebuddy.agent.ByteBuddyAgent.install(ByteBuddyAgent.java:248)
	at net.bytebuddy.agent.ByteBuddyAgent.install(ByteBuddyAgent.java:234)
	at org.mockito.internal.creation.bytebuddy.InlineByteBuddyMockMaker.<init>(InlineByteBuddyMockMaker.java:101)
	at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
	at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
	at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
	at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
	at java.lang.Class.newInstance(Class.java:442)
	at org.mockito.internal.configuration.plugins.PluginLoader.loadImpl(PluginLoader.java:81)
	... 11 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.ByteBuddyAgent.install(ByteBuddyAgent.java:301)
	... 21 more
Caused by: com.sun.tools.attach.AttachNotSupportedException: no providers installed
	at com.sun.tools.attach.VirtualMachine.attach(VirtualMachine.java:208)
	... 26 more

Environment:

  • Windows 7 (64 bit)
  • JDK 1.8.0_112 (64 bit)
java version "1.8.0_112"
Java(TM) SE Runtime Environment (build 1.8.0_112-b15)
Java HotSpot(TM) 64-Bit Server VM (build 25.112-b15, mixed mode)
  • Mockito 2.3.4
  • ByteBuddy 1.5.5 (transitive from Mockito)

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:16 (16 by maintainers)

github_iconTop GitHub Comments

3reactions
raphwcommented, Dec 16, 2016

I just debugged this on a virtual Windows box and I think I understand what happens:

First, everything went as expected. I could run the program and did not get the exception you experienced. I then installed an additional non-JDK JVM to see if the VM reproduced your problem. This was the case and I thought, this was your problem for sure.

To be certain, I retried it now with the JDK installation and it did no longer work. To my surprise, the installation had set some ..\ProgramData\Oracle\Java\... folder in my user’s hidden folder and overrode the JAVA_HOME property even if I set it to the proper place. I had to manually remove this folder and adjust my Windows PATH before I could run the above program with the installation specified at JAVA_HOME. Now, I could again switch between JDK and JVM.

The best part is that even without doing this, java -version shows the value of JAVA_HOME, thus indicates that you are running a JDK, not the JVM that you actually run. If you call System.gerProperty("java.home") from a program you run with the very same java.exe, you can however see the actual value.

The longer exception you see is a small issue in Byte Buddy that does however not change the outcome of the attachment. On Unix, if no JDK is available, Byte Buddy attempts to use its own attachment implementation. On Windows, this is impossible and would result in another IllegalStateException.

2reactions
vyazelenkocommented, Dec 16, 2016

@raphw you are absolutely right!

If I print JAVA_HOME variable I get:

P:\>echo %JAVA_HOME%
c:\Program Files\Java\jdk1.8.0_112\

But when I run test program which prints System.getProperty("java.home") I get the following:

java.home=C:\Program Files\Java\jre1.8.0_112

This happens, because I’m always installing JDK with JRE from the same installer. Installer first installs JDK and then installs JRE. What I do then is set JAVA_HOME to point to newly installed JDK and always include JAVA_HOME\bin into PATH variable. However when I print PATH variable I see the following:

P:\>echo %PATH%
C:\ProgramData\Oracle\Java\javapath
...
c:\Program Files\Java\jdk1.8.0_112\\bin
...

The very first entry is this magic C:\ProgramData\Oracle\Java\javapath which is apparently breaking everything.

Read more comments on GitHub >

github_iconTop Results From Across the Web

unit testing - Mockito Inline Mock Maker - throws Exception
unit testing - Mockito Inline Mock Maker - throws Exception - Argument passed to when() is not a mock - Stack Overflow. Stack...
Read more >
How to use the Mockito's inline mock maker - David Vlijmincx
With the inline mock maker, you can mock final classes and methods. You can do this using mock(SomeClazz.class) like you would with every...
Read more >
Mockito Inline Fails with "Java.lang.ExceptionInitializerError"
I am trying to mock a project that uses final classes. I have tried to implement the solution for mocking final classes using...
Read more >
Mockito 3.0.0 API - Javadoc.io
Be aware that you cannot use the inline mock maker on Android due to limitations in ... Use doThrow() when you want to...
Read more >
Mock Final Classes and Methods with Mockito - Baeldung
Learn how to mock final classes and methods with Mockito. ... MockMaker and add a single line of text: mock-maker-inline
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found