How to use expectNew when constructor is called with "this"?

1,662 views
Skip to first unread message

ebeb

unread,
Apr 27, 2010, 7:46:35 AM4/27/10
to PowerMock
Hi!

I have ha class "ClassB" that creates a new instance of "ClassB". As
an argument, it passes "this" as a reference to the parent class.

ClassA mock = PowerMock.createMock(ClassA.class);
PowerMock.expectNew(ClassA.class, ClassB.this).andReturn(mock);
PowerMock.replay(ClassA.class, futureMock);
ClassB instance = new ClassB();

The second line ClassB.this obviously does not work. Also ClassB.class
does not work because it compares a Class object with an instance of
this object which does not work. I can't pass the instance because it
is not created yet.

Something like: Which came first, the chicken or the egg? :)

Is this possible at all and if yes, then how?

Thanks!

--
You received this message because you are subscribed to the Google Groups "PowerMock" group.
To post to this group, send email to powe...@googlegroups.com.
To unsubscribe from this group, send email to powermock+...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/powermock?hl=en.

Johan Haleby

unread,
Apr 27, 2010, 4:09:54 PM4/27/10
to powe...@googlegroups.com
Hi,

Perhaps you can try using an argument matcher? I.e. something like:

expectNew(ClassA.class, isA(ClassB.class)).andReturn(..);

/Johan

Jan Wedel

unread,
Apr 28, 2010, 2:50:08 AM4/28/10
to powe...@googlegroups.com

Hi Johan!

 

Thank you for your reply. It did work with the EasyMock.isA() method! Great!

 

-Jan

Jan Wedel

unread,
May 19, 2010, 11:07:51 AM5/19/10
to powe...@googlegroups.com

Hi again!

 

I just had a more advanced use case for that problem, actually it was a constructor that takes two arguments.

 

When I tried

 

String name = “Foo”;

expectNew(ClassA.class, name, isA(ClassB.class)).andReturn(someMock);

 

I’ll get an IllegalStateException saying “2 matchers expected, 1 recorded.”. What does it actually mean? I assumed, that it can either  only take normal parameters or only matchers. Is that correct?

 

Cause when I changed it to

 

expectNew(ClassA.class, isA(String.class), isA(ClassB.class)).andReturn(someMock);

 

it works!

 

Is that correct or is there any chance to mix up params and machers?

 

/Jan

 

Von: Johan Haleby [mailto:johan....@gmail.com]
Gesendet: Dienstag, 27. April 2010 22:10
An: powe...@googlegroups.com
Betreff: Re: How to use expectNew when constructor is called with "this"?

 

Hi,

Johan Haleby

unread,
May 20, 2010, 2:21:12 AM5/20/10
to powe...@googlegroups.com
This is actually an easymock question, please read up on argument matchers in the easymock documentation.

/Johan

Jan Wedel

unread,
May 20, 2010, 2:37:36 AM5/20/10
to powe...@googlegroups.com

Oh, I wasn’t aware of that when I asked! You’re right. I read the EasyMock docs but couldn’t find an answer to my question if you can mix up matchers an objects but I guess the answer is no. At least I found EasyMock.same() to which I can pass the String “name” in my example and use it as a substitution.

 

String name = “Foo”;

expectNew(ClassA.class, same(name), isA(ClassB.class)).andReturn(someMock);

 

And it works.

Johan Haleby

unread,
May 20, 2010, 2:49:04 AM5/20/10
to powe...@googlegroups.com
The answer is "no". If you need an argument matcher for one parameter then you need to use argument matchers for all parameters. But there are several argument matchers available, usually you need eq(..) or same(..) for the others. 

/Johan
Reply all
Reply to author
Forward
0 new messages