Sunday, March 1, 2009

why we cant mock static methods

This wasn't obvious to me at first, it took me a while to get why we can't mock static methods. So here it goes the reason in one line,

"Static methods aren't polymorphic, they are resolved at compile time, therefor it's not possible to use mock objects to test."

Hope this help you guys who are scratching your head like I was.... (if you are not sure about polymorphism, think of it as late binding)

In my previous posts i showed one of the ways you can mock static methods, but the limitation is that you need to know the implementation of the class. What if i am using another classes static method whose cource code i dont know.

for example : FacesContext fc = facesContext.getInstance();

No comments: