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

a 夏天 提交于 2020-01-02 04:06:07

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/21846183/powermock-junit-tests-are-taking-more-time-to-execute-compared-to-normal-junit

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