Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I mock static methods in a class with easymock?

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

like image 806
JavaRocky Avatar asked Jul 02 '10 00:07

JavaRocky


People also ask

Can EasyMock mock static method?

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.

Can you mock a static method?

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.

Can we mock static methods using MOQ?

You can use Moq to mock non-static methods but it cannot be used to mock static methods.


1 Answers

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

like image 187
Ben J Avatar answered Sep 18 '22 13:09

Ben J