Using PowerMockito to mock static getInstance() class methods

2,132 views
Skip to first unread message

GeoffH

unread,
Nov 2, 2009, 11:59:03 AM11/2/09
to PowerMock
Johan,

Is there a way using PowerMockito to mock the static getInstance()
method of classes ?

So if there is a class

public final class MyFinalClassWithGetInstance {

// ensure class is not instantiated through constructor
private MyFinalClassWithGetInstance() {
}

public static MyFinalClassWithGetInstance getInstance() {
return new MyFinalClassWithGetInstance ();
}
}

Is there a way to mock the getInstance() method ?

A number of the classes I need to test have static initialisers using
getInstance() methods of other classes
Ideally it would be desirable to avoid changing existing code

Johan Haleby

unread,
Nov 2, 2009, 12:18:54 PM11/2/09
to powe...@googlegroups.com
Hi,

Hmm I think you mix up static initializers with static methods since I don't see any static initializers in your example? You can mock new instances and static methods with PowerMock+Mockito, have a look at this blog I wrote a couple of days ago for examples.

/Johan

GeoffH

unread,
Nov 2, 2009, 12:59:11 PM11/2/09
to PowerMock
Johan,

I'll try and demonstrate better what I mean below:

// class with getInstance() method
public final class MyFinalClassWithGetInstance {
private SomeObejct someObject;

// ensure class is not instantiated through constructor
private MyFinalClassWithGetInstance() {
}

public static MyFinalClassWithGetInstance getInstance() {
return new MyFinalClassWithGetInstance ();
}

public SomeObject getSomeObject() {
return someObject;
}
}

// class with static initialiser
public final class MyFinalClasswithStaticInitialiser1 {
private MyFinalClassWithGetInstance myClass1 =
MyFinalClassWithGetInstance.getInstance();

// this class might also have a getInstance() and private
constructor
private MyFinalClasswithStaticInitialiser1() {
}

public static MyFinalClasswithStaticInitialiser1 getInstance() {
return new MyFinalClasswithStaticInitialiser1();
}

// instance methods
}


// another class with static initialiser
public final class MyFinalClasswithStaticInitialiser2 {
private MyFinalClasswithStaticInitialiser1 myClass2 =
MyFinalClasswithStaticInitialiser1.getInstance();

// instance methods
}


If MyFinalClasswithStaticInitialiser2 is used as a member somewhere,
then before I can mock it, do I need to mock
MyFinalClasswithStaticInitialiser1 to set expectations for the
instance methods?

And before I can mock MyFinalClasswithStaticInitialiser1, do I need to
mock MyFinalClassWithGetInstance to set expectations for the instance
methods?

Is there a way to mock the getInstance method of
MyFinalClassWithGetInstance and do I need to ?

How would I stub the getSomeObject method of
MyFinalClassWithGetInstance ?

Thanks for your patience

GeoffH

On Nov 2, 5:18 pm, Johan Haleby <johan.hal...@gmail.com> wrote:
> Hi,
>
> Hmm I think you mix up static initializers with static methods since I don't
> see any static initializers in your example? You can mock new instances and
> static methods with PowerMock+Mockito, have a look at this
> blog<http://blog.jayway.com/2009/10/28/untestable-code-with-mockito-and-po...>I
> wrote a couple of days ago for examples.
>
> /Johan
>

Johan Haleby

unread,
Nov 3, 2009, 2:21:42 AM11/3/09
to powe...@googlegroups.com
Hi,

MyFinalClasswithStaticInitialiser1 doesn't have a static initializer as far as I can see. If the myClass1 field would have been static then it would have been assigned to the value of MyFinalClassWithGetInstance.getInstance() when the class is loaded. Now it gets assigned when "MyFinalClasswithStaticInitialiser1" is instantiated. So what you may want to do is to suppress the constructor of "MyFinalClasswithStaticInitialiser1". Again refer to the blog to see how this is done (there's also a link to an example i subversion so you can see that to prepare for test). Another option would be to suppress or stub the "getInstance" method of "MyFinalClassWithGetInstance". This is also mentioned in the blog (e.g. suppress(method(MyFinalClassWithGetInstance.class, "getInstance")).

Hope this may point you to the right direction
/Johan

GeoffH

unread,
Nov 3, 2009, 9:36:14 AM11/3/09
to PowerMock
Johan,

Thanks for your response

I meant the private members to have the static keyword
No matter, I have looked at your blog and there are some useful
examples which I have adapted to my needs

To be able to progress, I need to resolve the joda-time resource not
found problem when running with Maven 2
I posted some information in another topic, but am in the process of
creating minimal representative classes to recreate the problem

I'll create a new topic and upload the classes when ready

Thanks

GeoffH

On Nov 3, 7:21 am, Johan Haleby <johan.hal...@gmail.com> wrote:
> Hi,
>
> MyFinalClasswithStaticInitialiser1 doesn't have a static initializer as far
> as I can see. If the myClass1 field would have been *static *then it would
> have been assigned to the value of MyFinalClassWithGetInstance.getInstance()
> when the class is loaded. Now it gets assigned when
> "MyFinalClasswithStaticInitialiser1" is instantiated. So what you may want
> to do is to suppress the constructor of
> "MyFinalClasswithStaticInitialiser1". Again refer to the
> blog<http://blog.jayway.com/2009/10/28/untestable-code-with-mockito-and-po..>to
> see how this is done (there's also a link to an example i subversion
> so
> you can see that to prepare for test). Another option would be to suppress
> or stub the "getInstance" method of "MyFinalClassWithGetInstance". This is
> also mentioned in the blog (e.g.
> suppress(method(MyFinalClassWithGetInstance.class, "getInstance")).
>
> Hope this may point you to the right direction
> /Johan
>
Reply all
Reply to author
Forward
0 new messages