Suppose I have a class like so:
public class StaticDude{ public static Object getGroove() { // ... some complex logic which returns an object }; }
How do I mock the static method call using easy mock? StaticDude.getGroove()
.
I am using easy mock 3.0
EasyMock implements an interface at runtime, whereas Mockito inherits from the target class to create a mocking stub. Neither approach works well for static methods since static methods are associated with a class and cannot be overridden.
Since static method belongs to the class, there is no way in Mockito to mock static methods. However, we can use PowerMock along with Mockito framework to mock static methods.
You can use Moq to mock non-static methods but it cannot be used to mock static methods.
Not sure how to with pure EasyMock, but consider using the PowerMock extensions to EasyMock.
It has a lot of cool functions for doing just what you need - https://github.com/jayway/powermock/wiki/MockStatic
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With