Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8952731
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T13:56:21+00:00 2026-06-15T13:56:21+00:00

Say I have a class with the following code void MessageBuilder::Init(DBusMessage* pMsg) { if

  • 0

Say I have a class with the following code

void MessageBuilder::Init(DBusMessage* pMsg)
{
    if (NULL != m_pMsg)
    {
        ::dbus_message_unref(m_pMsg);
    }
    // m_pMsg is a private data member
    m_pMsg = pMsg;
    ::dbus_message_iter_init_append(m_pMsg, &m_objArgs);
}

The DBUS calls are in a namespace, hence the :: (I believe). Can anyone suggest how to mock/stub out the ::dbus_* calls?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T13:56:22+00:00Added an answer on June 15, 2026 at 1:56 pm

    With GoogleMock you can fake virtual methods of classes. But the ::dbus_* functions are not member of any class (you are right, they are in a namespace: the global namespace). So you cannot use GoogleMock (directly).

    James W. Grenning in Test-Driven Development for Embedded C suggests some solutions to your problem:

    1. Link-time substitution: don’t link the test code against the original library containing the ::dbus_* function code, but instead create new C/CPP files in the test project, which implement fakes of all ::dbus_* functions used by the component under test. Also add the component under test to the test project, so the linker will automatically resolve ::dbus_* calls to your fakes.
    2. Function pointer substitution: instead of using directly the ::dbus_* functions in your component under test use function pointers, which are initialized to ::dbus_* functions in production code and fake functions in test code.
    3. Preprocessor substitution: use #define to override names, e.g. #define dbus_message_unref(p) fake_dbus_message_unref(p) to make the component under test call your own fake function fake_dbus_message_unref(). This approach requires to add the component under test to the test project.

    If proposal 1. is feasible and does not create link problems, it is much less work than proposal 2. The cheesy part of proposal 3. is that it actually changes (!) the code of the component under test, so I would rather shy away from 3. Proposal 1. is the recommended way.

    Another alternative could be to use C++ wrapper classes with virtual methods around the ::dbus_* functions, so you can use a mocking tool like GoogleMock to fake them. Writing such wrappers would probably mean quite some effort. As short cut you could search for a DBUS C++ wrapper library — if you are lucky you find a mockable one.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Say I have the following code: public class ClassToTest { AnotherClass anotherClass; public void
Say I have the following code: class Parent { static string MyField = ParentField;
Let's say I have the following code: class MyClass(object): def __init__(self, param): self.param =
Let's say we have the following code in a class: //class code TextBox t
Lets say that I have the following code: public class Shelter<A extends Animal, B
Say suppose I have the following Java code. public class Example { public static
Let's say we have the following piece of code: public class Event { }
Say you have the following class public class AccessStatistics { private final int noPages,
Lets say I have the following code: public class Collection implements CollectionInterface{ ElementInterface[] elementArray
lets say i have a file MainClass.class which i think contains the following code

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.