Tag Archives: Powermock error

[Solved] Powermock error: Java.Lang.linkageerror: load constraint violation

Use powermock to report an error Java Lang. linkageerror: load constraint violation

Error reporting information (limited to deletion of space): analysis and solution extension

Error reporting information (limited to deletion of space):

java.lang.LinkageError: Loading constraint violation: loaders“org/powermock/core/classloader/javassist/JavassistMockClassLoader@47ab16ce”先前已启动由装入器“com/ibm/oti/vm/BootstrapClassLoader@ba626924”定义的名为“javax/security/auth/x500/X500Principal”的另一种类型的装入

\tat java.lang.ClassLoader.defineClassImpl(Native Method)
\tat java.lang.ClassLoader.defineClass(ClassLoader.java:346)
\tat org.powermock.core.classloader.javassist.JavassistMockClassLoader.loadUnmockedClass(JavassistMockClassLoader.java:90)
\tat org.powermock.core.classloader.MockClassLoader.loadClassByThisClassLoader(MockClassLoader.java:104)
\tat 、ock.core.MockInvocation.init(MockInvocation.java:31)
\tat org.powermock.core.MockInvocation.<init>(MockInvocation.java:22)
\tat org.powermock.core.MockGateway.doMethodCall(MockGateway.java:155)
\tat org.powermock.core.MockGateway.methodCall(MockGateway.java:138)

analysis

java.Lang.linkageerror this exception is often encountered in applications using multiple classloaders
the reason for this error is the class conflict caused by the cross use of class classes with the same qualified name after being loaded by multiple different classloaders. Classes with the same qualified name belong to different class instances in different classloaders. When loading a class, the JVM needs to load all imported classes. In this case, if there is a class with the same qualified name in the custom classloader as that in the parent classloader, Java. Net will be thrown Lang.LinkageError.
from the error message, you can see that the javax/security/auth/X500/X500 principal class is loaded by the classloader and bootstrapclassloader customized by powermock at the same time.

Solution:

Use @powermockignore to make javassistmockclassloader ignore the loading of javax/security/auth/X500/X500 principal:

@PowerMockIgnore({"javax.security.*"})