Mocking static classes

7 views
Skip to first unread message

Matthias Loitsch

unread,
Jul 13, 2010, 10:37:53 AM7/13/10
to snap...@googlegroups.com

Hi,

In the wiki, on this page: http://wiki.github.com/Jakobo/snaptest/mocking-static-classes the last paragraph isn't very clear to me...

It says, that, if you want to test an object, that calls a static function, you should refactor the call in a protected method, so you can use setReturnValue().

I'm assuming, that in the example given, a object of type foo is tested. So what I don't understand is: in this case, foo is not a mock object (obviously, since I want to test it), so how could I use setReturnValue() on it?


I hope my question is clear.....

Thanks in advance,

Matthias Loitsch
m...@tthias.com

Jakob Heuser

unread,
Jul 13, 2010, 1:23:24 PM7/13/10
to snap...@googlegroups.com
What it's saying is to isolate the factory method to a single function.

So instead of calling Foo::create("bar") in the middle of a method,
create a protected method instead to isolate the static call. This
allows you to use setReturnValue() normally to return mocked objects.

I've updated the docs as well.

Regards,
Jakob

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

Matthias Loitsch

unread,
Jul 13, 2010, 6:27:25 PM7/13/10
to snap...@googlegroups.com
Sorry, I still don't get it.....

I thought using setReturnValue() is only possible on mock objects.... But in this case would not be mocked, since it's the class I want to test...
I'm still missing something...

cheers,
matthias
--
Matthias Loitsch
matt...@loitsch.com

Matthias Loitsch

unread,
Jul 13, 2010, 6:29:33 PM7/13/10
to snap...@googlegroups.com
Oh sorry, just read your other E-Mail. I guess I just need to mock the class I want to test.

I just thought that mocking a class overwrites *all* methods, and returns null, but I guess there is a possibility to configure that behavior.

cheers!

On Tue, Jul 13, 2010 at 7:23 PM, Jakob Heuser <ja...@felocity.com> wrote:



--
Matthias Loitsch
matt...@loitsch.com

Reply all
Reply to author
Forward
0 new messages