Question

As i did some research i have found out that PowerMock is able to mock static java methods.

Can someone explain (technically) what is PowerMock doing different than JUnit and others which can not or do not? And also why static methods are(were) causing issues when they are tried to mock?

thanks

Was it helpful?

Solution

http://blog.jayway.com/2009/05/17/mocking-static-methods-in-java-system-classes/

In order to mock an instance method, you can simply override it in a subclass. You can't do that with static methods because there's no "static polymorphism".

Powermock can do it because it works with bytecode, while other popular frameworks rely on polymorphism and create subclasses with CGLIB.

From the link: "Basically all standard mock frameworks use CGLib to create a mock object which means that they're based on a hierarchical model (CGLib creates a sub class of the class to test at run-time which is the actual mock object) instead of a delegation model which PowerMock uses through it's byte-code manipulation by delegating to the MockGateway."

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top