Mock object instantiation and interaction

466 views
Skip to first unread message

AlexK

unread,
Nov 12, 2011, 8:55:23 PM11/12/11
to Google C++ Mocking Framework
Hello,
I created a couple of mock objects by hand recently and realized that
it would be a lot of work to add the ability to set expectations and
control return values in a robust way, so I hope I can use gMock
instead. But I'm probably missing something about how I can use gMock.
I'd like to test my code that currently instantiates a real object,
but I'd like this code to use a mock object instead. But I don't want
to modify this production code to instantiate a mock object intead of
the real one. And how can I set expectations on the mocked object
externally, from the unit test, when I don't even have access to the
real (or mock) object from the "outside" because it's instantiated
within the code I'd like to test?

This is how I set up the mock objects that I created by hand:
The first mock object I created (by hand) is a class with exactly the
same name as the real class. When building the unit test, instead of
linking with the real static library that provides the real class, I
just link with the object file of the mock class. The second mock
object I created (by hand) is a .so library that implements the same
interface as the real .so. At unit test execution time I use the
mock .so instead of the real one.

I'd appreciate if someone could please explain what I'm missing.
Thanks,
-Alex

Manuel Klimek

unread,
Nov 13, 2011, 4:18:02 AM11/13/11
to AlexK, Google C++ Mocking Framework
Hi Alex,

the way you describe how to replace the real class with the mock at
link time definitely is a lot of work. I personally wouldn't do that,
as there are (in my opinion better) alternative solutions:
- do not instantiate objects of classes you depend on in your class
under test, but create those objects outside the class and give them
to the class at construction time (this pattern is called "dependency
injection"). Obviously you don't want to do this for all classes (for
example, simple collections or strings). My personal rule of thumb is
that if I don't want to inject a dependency I probably don't want to
use mocks for testing (they introduce strong coupling on specific
interactions instead of the interface)
- if you don't want to create and hand in a mock object, but want to
usually do the setup and creation of the depended on class in your
class under test, create a way to be able to inject the mock object
after creation. This is slightly "dirty" in that it adds normally
unneeded clutter to your class under test, so use it only when the
other option is not available
- don't use mocks; mocks are not always the right way to test a class
- from your description I have no idea what the responsibility of the
class you want to mock is, so I can't give advice there

Cheers,
/Manuel

AlexK

unread,
Nov 13, 2011, 8:35:51 AM11/13/11
to Google C++ Mocking Framework
Hi Manuel,

Thank you for your suggestions. I think I may be able to refactor my
code to have a method that returns the object that I'd like to replace
with a mock object. Then in my test code I'll override this method to
return a mock object. I found this article useful:
http://www.ibm.com/developerworks/library/j-mocktest/index.html

Thanks again,
-Alex
Reply all
Reply to author
Forward
0 new messages