Jmock - How to mock a static method

maverik picture maverik · Sep 6, 2012 · Viewed 9.5k times · Source

Possible Duplicate:
jmock mocking a static method

I am working on a legacy unit test which uses static methods of a class XX. I have now changed the class to get a field value from Spring application context. The unit test now fail as the class XX cannot be initialized.

I cannot wrap the class XX with a dummysupport class since the logic to be tested is complex and the call to class XX is nested within multiple layers of calls to other legacy classes. So i am trying to find a way to mock the static methods within the class class XX. Is there a way do this? I am using Jmock library.

Answer

Jeff Olson picture Jeff Olson · Sep 6, 2012

There does not appear to be a way to mock static methods in JMock. See this related thread. The creators of JMock appear to take the elitist view that if you have static methods in your code, your code is unworthy to be tested using JMock.

I would suggest using a tool like JMockit that provides a more full-featured mocking toolset.