问题
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