0

I have a piece of C++ code whose unit testing is to be done. e.g.

//totest.h
#include "lowerlayer.h"
class ToTest
{
    LowerLayer *ll;
public:
    ToTest();
    void function_totest();
};

//totest.cpp
#include "totest.h"
ToTest::ToTest()
{
    ll = new LowerLayer();
}
void ToTest::function_totest()
{
    ll->function_lowerlayer();
}

//lowerlayer.h
class LowerLayer
{
public:
    LowerLayer();
    void function_lowerlayer();
};

//lowerlayer.cpp
#include "lowerlayer.h"
LowerLayer::LowerLayer()
{
    //do nothing
}
void LowerLayer::function_lowerlayer()
{
    //do nothing
}

The four files viz. totest.h, totest.cpp, lowerlayer.h and lowerlayer.cpp are the production code files are are not supposed to be modified for unit testing.

I have a test app whose code is as follows

//testcode.cpp
#include "totest.h"
int main()
{
    ToTest *tt = new ToTest();
    tt->function_totest();
    //some asserts
    return 0;
}

Now, I have to create a stub functionality for class LowerLayer and its functions. When function function_lowerlayer is called from function function_totest, both stub and real functions (one at a time) should be called using a controlling flag from testcode application.

Please provide some suggestions to design this requirement. Thanks, Ankur

user982740
  • 193
  • 2
  • 3
  • 9
  • What is stub "functionality"? A stub is a placeholder for a method that hasn't been completed, it generally contains comments as to how the method will be implemented and contains a dummy return value so the source file will compile. – Hunter McMillen Feb 11 '12 at 07:18
  • @HunterMcMillen - That isn't quite correct; stubs have specific meaning in a testing context. [Here's a nice article](http://martinfowler.com/articles/mocksArentStubs.html) explaining both `stubs` and `mocks` from a testing perspective. – dbn Dec 20 '12 at 23:02
  • This may be a duplicate of [Seeking code stub generator (from header files)](http://stackoverflow.com/q/2020568/1309332). – dbn Dec 20 '12 at 23:04
  • @dbw This post is 10 months old... – Hunter McMillen Dec 21 '12 at 03:58

2 Answers2

1

Try using GoogleMock in your tests. http://code.google.com/p/googlemock/

Inspired by jMock, EasyMock, and Hamcrest, and designed with C++'s specifics in mind, Google C++ Mocking Framework (or Google Mock for short) is a library for writing and using C++ mock classes. Google Mock:

lets you create mock classes trivially using simple macros, supports a rich set of matchers and actions, handles unordered, partially ordered, or completely ordered expectations, is extensible by users, and works on Linux, Mac OS X, Windows, Windows Mobile, minGW, and Symbian.

Paul Nikonowicz
  • 3,883
  • 21
  • 39
  • I tried using Mockpp framework and invested some time in that. But it didnot help me out. I am not sure of GoogleMock. Is there any other solution other than using a framework. – user982740 Feb 12 '12 at 00:21
  • well, you could write your own mocks, but using a framework is so much easier. – Paul Nikonowicz Feb 12 '12 at 16:42
  • For some reason, I thought Google Mock only worked on virtual or template classes. Is that not true? – dbn Dec 20 '12 at 02:48
0

Try using cpp stub https://github.com/coolxv/cpp-stub

//for linux and windows
#include<iostream>
#include "stub.h"
using namespace std;
int foo(int a)
{   
    cout<<"I am foo"<<endl;
    return 0;
}
int foo_stub(int a)
{   
    cout<<"I am foo_stub"<<endl;
    return 0;
}


int main()
{
    Stub stub;
    stub.set(foo, foo_stub);
    foo(1);
    return 0;
}
coolxv
  • 9
  • 1