Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PowerMockRunner with mock-maker-inline Classloader issues #1034

Open
arivazhaganj opened this issue Feb 24, 2020 · 0 comments · May be fixed by #1090
Open

PowerMockRunner with mock-maker-inline Classloader issues #1034

arivazhaganj opened this issue Feb 24, 2020 · 0 comments · May be fixed by #1090

Comments

@arivazhaganj
Copy link

Versions:
byte-buddy - 1.10.8
Mockito - 3.0.0
Powermock - 2.0.0

Scenario:
Test case involving PowerMock (to mock and spy static classes and methods)
Mockito2 -> Normal mocking and it is done on a Spring context as well.
mock-maker-inline configuration

Earlier issue of PowerMockRule + mock-maker-inline configuration issue is resolved as per
Stackoverflow link

After the above fix, Facing class loader issues when multiple test cases make use of similar configuration.
Additional discussions on byte-buddy github page here

Error Log:


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

	at org.mockito.internal.creation.bytebuddy.InlineByteBuddyMockMaker.<init>(InlineByteBuddyMockMaker.java:168) ~[mockito-core-3.0.0.jar:na]
	at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[na:1.8.0_161]
	at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) ~[na:1.8.0_161]
	at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) ~[na:1.8.0_161]
	at java.lang.reflect.Constructor.newInstance(Constructor.java:423) ~[na:1.8.0_161]
	at java.lang.Class.newInstance(Class.java:442) ~[na:1.8.0_161]
	at org.mockito.internal.configuration.plugins.DefaultMockitoPlugins.create(DefaultMockitoPlugins.java:68) ~[mockito-core-3.0.0.jar:na]
	... 62 common frames omitted
Caused by: java.lang.IllegalStateException: Error during attachment using: net.bytebuddy.agent.ByteBuddyAgent$AttachmentProvider$Compound@520f2ae1
	at net.bytebuddy.agent.ByteBuddyAgent.install(ByteBuddyAgent.java:613) ~[byte-buddy-agent-1.10.8.jar:na]
	at net.bytebuddy.agent.ByteBuddyAgent.install(ByteBuddyAgent.java:586) ~[byte-buddy-agent-1.10.8.jar:na]
	at net.bytebuddy.agent.ByteBuddyAgent.install(ByteBuddyAgent.java:538) ~[byte-buddy-agent-1.10.8.jar:na]
	at net.bytebuddy.agent.ByteBuddyAgent.install(ByteBuddyAgent.java:515) ~[byte-buddy-agent-1.10.8.jar:na]
	at org.mockito.internal.creation.bytebuddy.InlineByteBuddyMockMaker.<clinit>(InlineByteBuddyMockMaker.java:104) ~[mockito-core-3.0.0.jar:na]
	at java.lang.Class.forName0(Native Method) ~[na:1.8.0_161]
	at java.lang.Class.forName(Class.java:264) ~[na:1.8.0_161]
	... 63 common frames omitted
Caused by: java.lang.reflect.InvocationTargetException: null
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_161]
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_161]
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_161]
	at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_161]
	at net.bytebuddy.agent.Attacher.install(Attacher.java:106) ~[byte-buddy-agent-1.10.8.jar:na]
	at net.bytebuddy.agent.ByteBuddyAgent.install(ByteBuddyAgent.java:608) ~[byte-buddy-agent-1.10.8.jar:na]
	... 69 common frames omitted
Caused by: java.lang.UnsatisfiedLinkError: Native Library /usr/lib/jvm/java-8-oracle/jre/lib/amd64/libattach.so already loaded in another classloader
	at java.lang.ClassLoader.loadLibrary0(ClassLoader.java:1907) ~[na:1.8.0_161]
	at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1845) ~[na:1.8.0_161]
	at java.lang.Runtime.loadLibrary0(Runtime.java:870) ~[na:1.8.0_161]
	at java.lang.System.loadLibrary(System.java:1122) ~[na:1.8.0_161]
	at sun.tools.attach.LinuxVirtualMachine.<clinit>(LinuxVirtualMachine.java:342) ~[na:na]
	at sun.tools.attach.LinuxAttachProvider.attachVirtualMachine(LinuxAttachProvider.java:63) ~[na:na]
	at com.sun.tools.attach.VirtualMachine.attach(VirtualMachine.java:208) ~[na:na]
	... 75 common frames omitted

Suggestions based on response here

Please suggest if there is a fix available for this. Thanks in advance!

rxue added a commit to rxue/powermock that referenced this issue Jan 23, 2021
rxue added a commit to rxue/powermock that referenced this issue Jan 23, 2021
…ockito-inline dependency

Fix powermock#1034

The root cause of the powermock#1034 error is still in the class loader, `org.powermock.core.classloader.MockClassLoader`:

when executing a test annotated with `RunWith(PowerMockRunner.class)` where *mockito-inline* and *powermock-api-mockito2* coexists, the invoking of `MockClassLoader.getResources("mocito-extensions/org.mockito.plugins.MockMaker")` with `"mocito-extensions/org.mockito.plugins.MockMaker"` as agument returns an instance of `Enumeration<URL>` and first element is the URL of *mockito-inline* instead of the expected *powermock-api-mockito2* jar
rxue added a commit to rxue/powermock that referenced this issue Jan 23, 2021
…ockito-inline dependency

Fix powermock#1034

The root cause of the powermock#1034 error is still in the class loader, org.powermock.core.classloader.MockClassLoader:

when executing a test annotated with RunWith(PowerMockRunner.class) where mockito-inline and powermock-kapi-mockito2 coexists, the invoking of MockClassLoader.getResources with "mocito-extensions/org.mockito.plugins.MockMaker" as agument returns an instance of Enumeration<URL> where the first element is the URL of mockito-inline instead of that of expected powermock-api-mockito2 jar. This should be handled as a special case
rxue added a commit to rxue/powermock that referenced this issue Jan 23, 2021
…ine dependency

Fix powermock#1034

The root cause of the powermock#1034 error is still in the class loader, org.powermock.core.classloader.MockClassLoader:

when executing a test annotated with RunWith(PowerMockRunner.class) where mockito-inline and powermock-kapi-mockito2 coexists, the invoking of MockClassLoader.getResources with "mocito-extensions/org.mockito.plugins.MockMaker" as agument returns an instance of Enumeration<URL> where the first element is the URL of mockito-inline instead of that of expected powermock-api-mockito2 jar. This should be handled as a special case
rxue added a commit to rxue/powermock that referenced this issue Jan 24, 2021
…ine dependency

Fix powermock#1034

The root cause of the powermock#1034 error is still in the class loader, org.powermock.core.classloader.MockClassLoader:

when executing a test annotated with RunWith(PowerMockRunner.class) where mockito-inline and powermock-kapi-mockito2 coexists, the invoking of MockClassLoader.getResources with "mocito-extensions/org.mockito.plugins.MockMaker" as agument returns an instance of Enumeration<URL> where the first element is the URL of mockito-inline instead of that of expected powermock-api-mockito2 jar. This should be handled as a special case

Based on the document of ClassLoader, the method to override should be protected findResources in stead of the public getResources
rxue added a commit to rxue/powermock that referenced this issue Jan 24, 2021
…ne dependency

Fix powermock#1034

The root cause of the powermock#1034 error is still in the class loader, org.powermock.core.classloader.MockClassLoader:

when executing a test annotated with RunWith(PowerMockRunner.class) where mockito-inline and powermock-kapi-mockito2 coexists, the invoking of MockClassLoader.getResources with "mocito-extensions/org.mockito.plugins.MockMaker" as agument returns an instance of Enumeration<URL> where the first element is the URL of mockito-inline instead of that of expected powermock-api-mockito2 jar. This should be handled as a special case

Based on the document of ClassLoader, the method to override should be protected findResources in stead of the public getResources
rxue added a commit to rxue/powermock that referenced this issue Jan 24, 2021
Fix powermock#1034

The root cause of the powermock#1034 error is still in the class loader, org.powermock.core.classloader.MockClassLoader:

When executing a test annotated with RunWith(PowerMockRunner.class) where mockito-inline and powermock-kapi-mockito2 coexists, the invoking of MockClassLoader.getResources with "mocito-extensions/org.mockito.plugins.MockMaker" as agument returns an instance of Enumeration<URL> where the first element is the URL of mockito-inline instead of that of expected powermock-api-mockito2 jar. This should be handled as a special case

Based on the document of ClassLoader, the method to override should be protected findResources in stead of the public getResources
rxue added a commit to rxue/powermock that referenced this issue Jan 24, 2021
Fix powermock#1034

The root cause of the powermock#1034 error is still in the class loader - org.powermock.core.classloader.MockClassLoader:

When executing a test annotated with RunWith(PowerMockRunner.class) where mockito-inline and powermock-kapi-mockito2 coexists, the invoking of MockClassLoader.getResources with "mocito-extensions/org.mockito.plugins.MockMaker" as agument returns an instance of Enumeration<URL> where the first element is the URL of mockito-inline instead of that of expected powermock-api-mockito2 jar. This should be handled as a special case

Based on the document of ClassLoader, the method to override should be protected findResources in stead of the public getResources
rxue added a commit to rxue/powermock that referenced this issue Jan 31, 2021
… issues

The root cause of the error is still in the class loader - org.powermock.core.classloader.MockClassLoader:

When executing a test annotated with RunWith(PowerMockRunner.class) where mockito-inline and powermock-api-mockito2 dependencies coexists, the invoking of MockClassLoader.getResources with "mocito-extensions/org.mockito.plugins.MockMaker" as argument returns an instance of Enumeration<URL> whose first element is the URL of mockito-inline. So the class loader will load the unexpected InlineByteBuddyMockMaker, which is defined in the file mocito-extensions/org.mockito.plugins.MockMaker. Whereas when executing a test annotated with RunWith(PowerMockRunner.class), the needed MockMaker should be based on the mocito-extensions/org.mockito.plugins.MockMaker file from the powermock-api-mockito2.jar: https://github.com/powermock/powermock/blob/release/2.x/powermock-api/powermock-api-mockito2/src/main/resources/mockito-extensions/org.mockito.plugins.MockMaker

As per the implementation, based on the document of ClassLoader, the method to override should be the protected findResources in stead of the public getResources and, the current implementation is just an extra if condition. In future if more if condition is to be added, refactor will be needed
rxue added a commit to rxue/powermock that referenced this issue Jan 31, 2021
… issues

The root cause of the error is still in the class loader - org.powermock.core.classloader.MockClassLoader:

When executing a test annotated with RunWith(PowerMockRunner.class) where mockito-inline and powermock-api-mockito2 dependencies coexists, the invoking of MockClassLoader.getResources with "mockito-extensions/org.mockito.plugins.MockMaker" as argument returns an instance of Enumeration<URL> whose first element is the URL of mockito-inline. So the class loader will load the unexpected InlineByteBuddyMockMaker, which is defined in the mockito-inline jar. Whereas when executing a test annotated with RunWith(PowerMockRunner.class), the needed MockMaker should be based on the mockito-extensions/org.mockito.plugins.MockMaker file from the powermock-api-mockito2 jar: https://github.com/powermock/powermock/blob/release/2.x/powermock-api/powermock-api-mockito2/src/main/resources/mockito-extensions/org.mockito.plugins.MockMaker

As per the implementation, based on the document of ClassLoader, the method to override should be the protected findResources in stead of the public getResources and, the current implementation is just an extra if condition. In future if more if condition is to be added, refactor will be needed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
1 participant