Tuesday, November 3, 2009

Automatic Mock Object For C++

Two or three years ago when I dive to Java for a half a year I studied Mock objects. At Java (or C#) it build around a reflection. During these years I discussed with my friends and colleagues about C++ Mock object libraries. A sharp corner of the discussions was a possibility to write powerful C++ Mock library without a reflection.
If you see any of existent C++ Mock implementations you find it requires to implement every mock object class by hands. Afterwards you can adjust its behavior at runtime like in Java. My opinion was to use code generation to implement a Mock. I even started to write some code with this concept in mind...
Another alternative is try to use C++ generic programming technique to emulate some reflection features at compile time. But it seemed hard and may not enough. Any case you cannot enumerate interface methods :(
There was some time while I found AMOP - Automatic Mock Object For C++. It is a very young project (first download became available at January'2008). I looked at the AMOP description, decided it very interesting to try but didn't find time to dive inside. I had think it need much time to understand the code and setup it for my project :(
Several days ago I found a hour to try it because I face task to write a really isolated test.
I was surprised how easy it can be. Just single include and library contains three cpp files! No any code generation, just instantiate a template and write a scenario like in Java!

Would you think it is possible to have such a lightweight solution with C++?.. Now I can answer - yes! See yourself at AMOP wiki.

PS: I am not a guy who like C# or even Java! I really like C++. I like it because it allows to do what you want and what you need in many ways. The AMOP example shows the power of C++. C++ may cover solutions you would never have thought.

2 comments:

  1. Thanks ^^, i am the creater of AMOP, but i rarely update right now...:)

    ReplyDelete
  2. Thank you for your work on AMOP. I think it has great future! :-)

    ReplyDelete