Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Powermock JUnit tests are taking more time to execute compared to normal JUnit

we are using powermock for mocking the static methods by using the @PrepareForTest annotations. The test runs fine but the problem is it is taking more time to execute the test.

The code snippet is like below:

@PrepareForTest({ StaticClass1.class, StaticClass2.class })

Normally, the JUnit with out mocking the static method is taking around 2 sec time to execute but when we add @PrepareForTest annotation for mocking the static calls, the test is taking around a minute time to complete the execution. For one test, this is not a big impact, but we have more than 1K tests and the overall build time is taking more than 2 hrs which earlier took around 20 - 30 minutes.

What could be the reason for the powermock taking so much of time.

Any help would be greatly appreciated.

like image 672
Srikanth Avatar asked Feb 18 '14 06:02

Srikanth


1 Answers

PowerMock is a tool that performs bytecode instrumentation on your generated classes. Once your classes are compiled into Java Bytecode, PowerMock comes along and modifies this generated classes. It is not possible otherwise (by using Java Proxy) to mock a private or a static method.

You should only use @PrepareForTest annotation only in those classes that you want to use features exclusive to PowerMock, such as mocking private or static methods.

like image 169
Random42 Avatar answered Oct 12 '22 05:10

Random42