Mockito + PowerMock LinkageError while mocking system class

岁酱吖の 提交于 2019-11-29 18:54:04

Try adding this annotation to your Test class:

@PowerMockIgnore("javax.management.*")

Worked for me.

烬哥哥

Classloader conflict, use this: @PowerMockIgnore("javax.management.*")

Let mock classloader do not load javax.*. It works.

Similar to the accepted response here, I ended up having to exclude all of the SSL related classes:

@PowerMockIgnore({"javax.management.*", "org.apache.http.conn.ssl.*", "com.amazonaws.http.conn.ssl.*", "javax.net.ssl.*"})

Adding that to the top of my class resolved the error.

This may be a bit of an old topic, but I have also ran into this problem. Turns out that some of the java versions cannot handle powermockito when powermock finds out there are 2 classes with the same name in the same package (over different dependencies).

With any version higher than Java 7_25 it gives this error.

ash

In order to mock system classes, prepare the class that is the target of the test, not Thread.class. There's no way PowerMock will be able to instrument Thread.class because it is required during JVM startup - well before PowerMock can instrument.

The way instrumentation works, once a class is loaded, it can no longer be intstrumented.

See the PowerMock wiki.

In PowerMock 1.7.0 a user-defined global configuration can be added to your project's classpath. PowerMockConfig

org/powermock/extensions/configuration.properties

Simply add a line in the properties file like:

powermock.global-ignore=javax.management.*

This will resolve the error for all the test classes in your project.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!