Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mockito + PowerMock LinkageError while mocking system class

People also ask

Can Mockito and PowerMock be used together?

Of course you can – and probably will – use Mockito and PowerMock in the same JUnit test at some point of time.

What is the difference between Mockito and PowerMock?

While Mockito can help with test case writing, there are certain things it cannot do viz:. mocking or testing private, final or static methods. That is where, PowerMockito comes to the rescue. PowerMockito is capable of testing private, final or static methods as it makes use of Java Reflection API.

Is PowerMock a mocking framework?

PowerMock is an open-source mocking library for Java applications. It extends the existing mocking frameworks, such as EasyMock and Mockito, to add even more powerful features to them. PowerMock enables us to write good unit tests for even the most untestable code.

What is PowerMock in Mockito?

PowerMock is an open-source Java framework used for creating a mock object in unit testing. It extends other mocking frameworks such as EasyMock and Mockito to enhance the capabilities.


Try adding this annotation to your Test class:

@PowerMockIgnore("javax.management.*")

Worked for me.


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.


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

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


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.


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.