Using mockito to mock classes initialized within methods

3,998 views
Skip to first unread message

Sutapa Dey

unread,
Jan 6, 2012, 7:12:59 AM1/6/12
to mockito
Hi All,

I am a newbie in unit testing with JUnit and started with Mockito and
found it very useful.

I was curious to know if it is possible to mock classes initialized
with the method I am writing the junit for. For example as below,
method toTest()
{

ClassA newA = new ClassA(); //Can I mock this when writing junit for
toTest method
newA.someMethod();


}

Regards,
Sutapa

leif hanack

unread,
Jan 6, 2012, 7:23:48 AM1/6/12
to moc...@googlegroups.com

Hi.
If you can't refactor such code take a look at powermock and jmockit.

Regards leif

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

Tomek Kaczanowski

unread,
Jan 6, 2012, 7:26:56 AM1/6/12
to moc...@googlegroups.com
Please read this discussion:
http://groups.google.com/group/mockito/browse_thread/thread/ef0617e3fda6e8d5#

It explains this topic in details.

--
Regards / Pozdrawiam
Tomek Kaczanowski
http://kaczanowscy.pl/tomek

2012/1/6 Sutapa Dey <suta...@gmail.com>:

Brice Dutheil

unread,
Jan 6, 2012, 12:18:22 PM1/6/12
to moc...@googlegroups.com
Oh yeah, thx Tomek; the explanation of David Wallace is first class.
Maybe we should put it in the wiki :P

-- 
Brice Dutheil

Tomek Kaczanowski

unread,
Jan 6, 2012, 1:53:18 PM1/6/12
to moc...@googlegroups.com
2012/1/6 Brice Dutheil <brice....@gmail.com>:

> Oh yeah, thx Tomek; the explanation of David Wallace is first class.
> Maybe we should put it in the wiki :P
I would really consider this. Mocking "new" is a recurring subject.

Szczepan Faber

unread,
Jan 9, 2012, 7:03:57 PM1/9/12
to moc...@googlegroups.com
We can do it at some point but I don't think it will ever top the priority list because:

-powermockito already does it
-there's an easy workaround, e.g. a factory, which would be healthy for the design anyway.

Cheers!
Szczepan Faber
Principal engineer@gradleware
Lead@mockito

Tomek Kaczanowski

unread,
Jan 10, 2012, 2:52:29 AM1/10/12
to moc...@googlegroups.com
Szczepan, I think you misunderstood. I wasn't asking for enhancing
Mockito with some new features. I was suggesting to enhance the
documentation on how to deal with "new" (and such) in Mockito way.

--
Regards / Pozdrawiam
Tomek Kaczanowski
http://kaczanowscy.pl/tomek


2012/1/10 Szczepan Faber <szcz...@gmail.com>:

Szczepan Faber

unread,
Jan 10, 2012, 11:53:18 AM1/10/12
to moc...@googlegroups.com
Ufff....

Thanks... :)

David Wallace

unread,
Jan 10, 2012, 8:20:38 PM1/10/12
to moc...@googlegroups.com
Thanks Brice, nice of you to say.  But let’s correct the error in it first.  There’s one point in there where I use when ... thenReturn on a spy; this should of course be doReturn ...
 
Shall I make this into a wiki page?

Brice Dutheil

unread,
Jan 11, 2012, 3:31:30 AM1/11/12
to moc...@googlegroups.com
In my opinion yes :)
-- 
Brice
Reply all
Reply to author
Forward
0 new messages