Mocking non-virtual functions without modifying the production code

5,349 views
Skip to first unread message

nbu

unread,
Oct 12, 2010, 11:58:43 AM10/12/10
to Google C++ Mocking Framework
Hi,

I'd like to mock some classes whose methods are not virtual, except
the destructor. The problem is that I can't modify the production
code.

I've read the CookBook, but I can't templatize, make inerfaces or
virtualize.

Even if I write fakes, I'd like to delegate calls to the true class,
but I guess it is impossible.

Is there anything is GoogleMock that could help me?

Best regards
Nico

Manuel Klimek

unread,
Oct 12, 2010, 1:13:26 PM10/12/10
to nbu, Google C++ Mocking Framework
If you have a hard to test class that you cannot change, you can
always write an adapter/facade:
You write a class that exports exactly the interface you need and
calls the methods on the unchangeable class.

As a side effect, the new adapter class will have only the methods you
need, in case the class you cannot change has a bigger interface.

Cheers,
/Manuel

nbu

unread,
Oct 13, 2010, 6:52:08 AM10/13/10
to Google C++ Mocking Framework
Sorry, I need more explanations (newbie here...). My code is in C++. I
have two cases, in both I can't change the production code:

-Testing a Class A, I need to make a mock for the tested class A,
whose methods are not virtual, then calling mocked methods in some
cases, or delagating to the true class in other cases

-Testing a Class A which uses a Class B (which is passed in the
construcor A), I need to make a mock for the class B whose methods are
not virtual. I need to delegate calls to the true class B but always
for the same methods

If possible, could you write a short example to explain how this
adpter could help?

Thank you

Cheers
Nico

On 12 oct, 19:13, Manuel Klimek <kli...@google.com> wrote:
> If you have a hard to test class that you cannot change, you can
> always write an adapter/facade:
> You write a class that exports exactly the interface you need and
> calls the methods on the unchangeable class.
>
> As a side effect, the new adapter class will have only the methods you
> need, in case the class you cannot change has a bigger interface.
>
> Cheers,
> /Manuel
>

Manuel Klimek

unread,
Oct 13, 2010, 11:54:41 AM10/13/10
to nbu, Google C++ Mocking Framework
Ok, so you're testing a class you cannot change (as opposed to code
using that class, which you can change).

The only way I know to do that is not using mocks, sorry. What helped
me in the past is Michael Feather's book "Working Effectively with
Legacy Code" (http://www.amazon.com/Working-Effectively-Legacy-Michael-Feathers/dp/0131177052),
which has many detailed examples / patterns how to test code with
minimal changes.

Cheers,
/Manuel

Keith Ray

unread,
Oct 13, 2010, 11:51:19 PM10/13/10
to Manuel Klimek, nbu, Google C++ Mocking Framework
Another way to test code that you cannot change is use tools that
modify the executable and/or change how the executable is built (most
of these tools are Windows-only)

Isolator++
*http://www.typemock.com/isolatorpp-product-page*

RudeMocks
http://www.martinecker.com/wiki/index.php?title=RudeMocks/Introduction

http://www.parasoft.com/jsp/products/cpptest.jsp

Modifying code to make it testable (done in the right way) also makes
the code better-designed: more modular, explicit dependencies instead
of implicit dependencies, classes and methods that don't violate the
Single Responsibility Principle. And it's relatively free. Some of the
solutions above cost a lot of money.

Of course, the same programmer who wrote or modified the code should
be testing it, otherwise they are not learning how much damage their
poor design does to testability.

--
C. Keith Ray
 Web: http://industriallogic.com
 Twitter: @CKeithRay, @IndustrialLogic

Amplify Your Agility
Coaching | Training | Assessment | eLearning

Reply all
Reply to author
Forward
0 new messages