Mock non virtual functions

2,001 views
Skip to first unread message

P Kaur

unread,
Apr 30, 2012, 11:47:27 PM4/30/12
to cpputest
Hi ,

I'd like to mock some classes whose methods are not virtual in
production code. Is there any way that CppUMock can mock non virtual
functions? The problem is that I could not modify production code
library.

Any ideas or hint will be helpful.

Thanks,
PKaur

Bas Vodde

unread,
May 1, 2012, 1:26:00 AM5/1/12
to cppu...@googlegroups.com

Hi,
Nope, there isn't a very good way to do that in C++. I usually recommend to just make the method virtual in the production code.

The only other alternatives are kinda hacky in which you could create a linker symbol of that function and make sure you link that instead of the real linker symbol.
I wouldn't go there myself though :)

Any reason why adding virtual isn't an option?

Bas

>
> Thanks,
> PKaur

Bas Vodde

unread,
May 1, 2012, 1:28:43 AM5/1/12
to cppu...@googlegroups.com

Oh, one alternative is also to not call that function :)

You can call, instead, a virtual function that does nothing except to call that function, then you can mock out the other function :)

Something like this (coded in email):

class prodCode
{
public:
void nonVirtualFunction();
};

class myCode
{
public:
void methodUnderTest() {
// nonVirtualFunction is the one I originally want to call, but it is non-virtual
virtualVersionOfNonVirtualFunction();
}

virtual void virtualVersionOfNonVirtualFunction() {
a->nonVirtualFunction();
}
}

This way you wrap around the function that you can't mock. Does that help?

Bas

P Kaur

unread,
May 1, 2012, 1:50:03 AM5/1/12
to cppu...@googlegroups.com
Thanks for the reply. I would definitely try second option through virtual wrapper method.

I am using CppUMock functionality for Qt Framework (Nokia). I am using CppUMock to test Qt classes. There are lot of methods that are non virtual. I can test virtual methods by sub classing Qt Production classes.

Bas Vodde

unread,
May 1, 2012, 1:52:14 AM5/1/12
to cppu...@googlegroups.com

Hi,

Yah, are you developing using QT or developing QT itself?

Bas

P Kaur

unread,
May 1, 2012, 2:00:03 AM5/1/12
to cppu...@googlegroups.com
Hi Bas,

I am creating applications using Qt. No I am not developing Qt.  Basically I am doing unit testing of project written based on Qt framework.

Bas Vodde

unread,
May 1, 2012, 2:02:16 AM5/1/12
to cppu...@googlegroups.com

Hi,

Oki, well, in that case, my approach would be to not link QT at all :) Just remove the QT library from the linker.

This way you can implement a fake QT library and mock out all of the QT functions you use easily.

I'd recommend this as you probably always want to mock out a call to QT.

Thanks,

Bas

Terry Yin

unread,
May 1, 2012, 2:35:39 AM5/1/12
to cppu...@googlegroups.com
What a good example for using 3rd party library!
--
-terry
-------------------------
Blog: http://terryyin.blogbus.com/
twitter: http://twitter.com/terryyin

Bryce Schober

unread,
May 1, 2012, 11:35:00 AM5/1/12
to cppu...@googlegroups.com
I've no experience with it, but Qt does have its own unit-testing library, QTestLib: http://qt-project.org/doc/qt-4.8/qtestlib-manual.html. Depending on how qt-centric your application is, that might end up being a better solution than CppUTest in your case.

<><  <><  <><
Bryce Schober

Bas Vodde

unread,
May 1, 2012, 8:50:58 PM5/1/12
to cppu...@googlegroups.com

Well, personally I would avoid that for unit testing. It seems it goes through the UI, so it might make a good integration or acceptance test framework :)

When doing pure unit tests, I'd always prefer to not use 3rd party libraries at all, so that when a test fails I'm sure it is my code and not others code. Also it keeps the tests fast. So, not using QT at all (creating a linker stub) is my preference :)

Anyways, good to know QT has some test library :)

Bas
Reply all
Reply to author
Forward
0 new messages