CppUMock returnPointer( )

1,292 views
Skip to first unread message

BGill

unread,
May 1, 2012, 10:55:48 PM5/1/12
to cpputest
Hi Bas,

I recently started usiing CppUTest Mock support. I am having trouble
with getPointerValue(). I am not quite sure how i am supposed to call
method who returns pointer object. I am using following function call
from CppUMock tool.

MockFunctionCall& withParameter(const SimpleString& name, const char*
value);

I am getting compiler error with method ProductionFile * openFile()
because it returns pointer object.

Here is the example of my code.

class FileInterface : public ProductionFile
{
public:
virtual std::string getFileName(){ return ""; }
virtual ProductionFile * openFile() = 0;
};
//Create Mock
class FileInterfaceMock: public FileInterface
{
public:
std::string getFileName()
{
std::string str
return str =
mock("FileInterface").actualCall("getFileName").returnValue().getStringValue();
}

//Problem with function
ProductionFile * openFile()
{
//getStringValue returns void *;
void *voidPtr =
mock("FileInterface").actualCall("openFile").returnValue().getStringValue();
//Is it correct and expected way to
ProductionFile *pFile = (ProductionFile *)voidPtr;
return pFile;
}
};

//Use file
class FileInterfaceContainer
{
public:
FileInterface * m_FileInterface;
FileInterfaceContainer(FileInterface *FileInterface)
{
m_FileInterface =FileInterface;
}
std::string getFileName()
{
return m_FileInterface->getFileName();
}

ProductionFile * OpenFile()
{
return m_FileInterface->open();
}


};

//This is my test class
//I am getting compiler error when calling openFile
void TestClass::TestGetFileName()
{
FileInterfaceMock pMock;
FileInterfaceContainer FileInterfaceContainer(&pMock);

//Following works Expected to return string file name on
getFileName() call.

mock("FileInterface").expectOneCall("getFileName").andReturnValue("file.txt");

//How I am supposed to pass pointer
//Error on following line andReturnValue(ProductionFile *);

mock("FileInterface").expectOneCall("openFile").andReturnValue(ProductionFile
*);

std::string str =FileInterfaceContainer.getFileName();
//Some more stuff
mock("FileInterface").checkExpectations();
}

Bas Vodde

unread,
May 1, 2012, 11:05:27 PM5/1/12
to cppu...@googlegroups.com

Hiya,

Uhm, well... I think you are almost there. But before that, let me explain the difference between getStringValue and getPointerValue... none :)

The main difference is conceptual. When you pass a string, usually you use GetStringValue and do AndReturnValue("string") or anything that is of type constant char.
When you pass any kind of other pointer, usually you'd want to use GetPointerValue instead. So in the case of the openFile, I'd use GetPointerValue in the mock rather than the GetStringValue (though, there is in practice, not much difference).

Then on the exceptOneCall for the OpenFile, you'd need to return something of a pointer type. So, you want to create the ProductionFile in your test. It could be a real ProductionFile or a subclassed one that is a mock. You create it in the test and then do andReturnValue(mockedProductionFile).

It ought to match to the void* call, if it doesn't or gives a warning then you add the (void*) cast to the call.

Does that help at all?

Thanks!

Bas

BGill

unread,
May 1, 2012, 11:25:08 PM5/1/12
to cpputest
I get following error in TestClass in expectOneCall ()

error C2664: 'MockFunctionCall::andReturnValue' : cannot convert
parameter 1 from 'FileInterfaceMock' to 'void *'


FileInterfaceMock pMock;
FileInterfaceContainer FileInterfaceContainer(&pMock);


//Here's the error C2664

mock("FileInterface").expectOneCall("openFile").andReturnValue(pMock);

Here's production file Mock same code as above. I changed it to
getPointerValue in openFile()

class FileInterfaceMock: public FileInterface
{


ProductionFile * openFile()
{
//getStringValue returns void *;
void *voidPtr =
mock("FileInterface").actualCall("openFile").returnValue().getPointerValue();
//Is it correct and expected way to
ProductionFile *pFile = (ProductionFile *)voidPtr;
return pFile;
}
};

Thanks for quick feedback.

Bas Vodde

unread,
May 1, 2012, 11:28:59 PM5/1/12
to cppu...@googlegroups.com

Hiya,

Oki, just change the call to

mock("FileInterface").expectOneCall("openFile").andReturnValue(&pMock);

(add a & to make it a pointer)

Thanks,

Bas

BGill

unread,
May 1, 2012, 11:37:51 PM5/1/12
to cpputest
Thanks Bas. I missed very silly thing. Test case works fine according
to our expectations. Great tool CppUTest. Just one more question I was
reading gMock documentation other day. We were trying to select unit
testing framework for the development project. They have returns value
as ReturnRef(), ReturnsNew () stuff . Is it available in CppUMock like
we can return reference or it is not part of tool?

Bas Vodde

unread,
May 1, 2012, 11:46:29 PM5/1/12
to cppu...@googlegroups.com

Hiya,

> Thanks Bas. I missed very silly thing. Test case works fine according
> to our expectations. Great tool CppUTest. Just one more question I was
> reading gMock documentation other day. We were trying to select unit
> testing framework for the development project. They have returns value
> as ReturnRef(), ReturnsNew () stuff . Is it available in CppUMock like
> we can return reference or it is not part of tool?

Nope, the CppUMock framework is quite a lot simpler than the GMock framework. It has been on purpose as it doesn't try to solve all problems. The effect is that mocking using CppUMock is a bit more verbose than using GMock. Especially as there are currently no macros for e.g. making mocking easier.

The disadvantage of GMock is also its advantage. The use of "advanced" templates sometimes makes things really complicated :) I spend once a day debugging something weird with very stange errors as it was hard to figure out what was going on. Also, mocking global functions in GMock is nearly impossible, which is quite a common thing, at least for products I work with :)

But, not too long ago, we made a CppUTest/GMock integration, which means you can just include the "CppUTest/GMock.h" and then you can use Google Mock for the things that it is good at, while still using CppUTest and CppUMock for the things that that is good at. I used this myself where I used GMock for mocking interfaces and CppUMock for mocking 3rd party libraries :)

Hope that helps.

Bas

BGill

unread,
May 2, 2012, 12:03:15 AM5/2/12
to cpputest
I completely agree with you. It is really hard to figure out anything
on gMock code base. It is too complicated even to understand core
functionality. But It's just my opinion. I may have very limited
knowledge of gmock.

Thanks lot for feedback. We have been using CppUTest from last month
and so far enjoying working on it. Best thing about cppu mock tool is
simplicity and easier to use not too complex.
Reply all
Reply to author
Forward
0 new messages