Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PowerMock & Java 11

Tags:

We are using PowerMock in few of our historical projects. Unfortunately PowerMock is quite dead and is not compatible with Java 11.

And we are using mockStatic(). Yes, we know its considered harmful - its in the legacy code and we would prefer not to rewrite those classes now...

Are there any options how to tweak PowerMock to support Java 11? Or is it possible to easily replace it with some other Java 11 compatible framework? (Mockito does not support mockStatic)

like image 899
malejpavouk Avatar asked Oct 24 '18 10:10

malejpavouk


People also ask

What is PowerMock used for?

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.

What is 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.

Why PowerMock is not recommended?

Power Mock gives you access to mock static methods, constructors etc. and this means that your code is not following best programming principles. Power Mock should be used in legacy applications where you cannot change the code which has been given to you.

Is Mockito PowerMock Which is better?

The division of work between the two is that Mockito is kind of good for all the standard cases while PowerMock is needed for the harder cases. That includes for example mocking static and private methods.


2 Answers

After one year of no releases, things are really moving in PowerMock.

PowerMock 2.0.0-RC1 was released. And with PowerMockito 2.0.0-RC1 + @PowerMockIgnore({"com.sun.org.apache.xerces.*", "javax.xml.*", "org.xml.*", "org.w3c.*"})

The tests work under Java 11.

like image 105
malejpavouk Avatar answered Sep 20 '22 21:09

malejpavouk


Resolved with:

@PowerMockIgnore("jdk.internal.reflect.*") 

Nothing else needed.

like image 44
Bruno Bossola Avatar answered Sep 19 '22 21:09

Bruno Bossola