4

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.

Community
  • 1
  • 1
maverik
  • 3,409
  • 3
  • 16
  • 7

2 Answers2

5

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.

Community
  • 1
  • 1
Jeff Olson
  • 6,323
  • 2
  • 22
  • 26
  • oh... In that case can Jmock and Jmockit work togather. The existing tests use jmock to test the functionalities. Now if i mock the class XX with JmockIt and can the other mock objects (Jmock) use this? – maverik Sep 06 '12 at 17:54
  • 2
    Wow, @Jeff, I think it's a bit of a stretch to call them "elitists" for suggesting that overuse of statics is *sometimes* an indication of a design problem. And I am no apologist for JMock (never used it). It just doesn't fit in with their design approach, which is not at all an elitist statement, even if you disagree with their reasons. "Unworthy to be tested..." I'll give you a +1 for drama and humor though. – Kevin Welker Sep 06 '12 at 18:25
  • 3
    @Kevin...No, I think it's pretty accurate. A lot of the mocking frameworks out there make statements like Steve Freeman did in the linked question: "We don't support mocking static methods in jMock because it doesn't fit our design approach". I think that is elitist...basically saying to users who are trying to test code with statics, sorry but you are doing it wrong. – Jeff Olson Sep 06 '12 at 21:07
  • @maverik - yes you could mix the two frameworks if you want. Or just JMockit (or PowerMock as RNJ mentioned) for everything, since JMockit will do everything you need. – Jeff Olson Sep 06 '12 at 21:09
  • @JeffOlson It would be interesting to see their response if you submit a patch that offers static method support. If they turn it down, then perhaps they are elitist. If they accept it, then perhaps they are just testing enthusiasts who target their own personal time at furthering their preferred methodology. Not a crime in my eyes. – Duncan Jones Sep 07 '12 at 17:16
1

I belive powermock allows you to mock statics as detailed here

I have also had this problem in the past and have managed to code around it so I can use JMock by making the method non static but having a static reference to the class.

For example

public ClassToMock {
    public static final ClassToMock INSTANCE = new ClassToMock();

    private ClasstToMock() {};

    public void newNonStaticMethod1(){}
}

instead of

public ClassToMock {

    public ClasstToMock() {};

    public void static origStaticMethod1(){}
}

Now your method call would be

ClassToMock.INSTANCE.newNonStaticMethod1();

as newNonStaticMethod1() is notn static you can now mock this.

As the CalssToMock ctor is private it can only be accessed through the static instance.

RNJ
  • 15,272
  • 18
  • 86
  • 131