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 8287329
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T12:00:18+00:00 2026-06-08T12:00:18+00:00

New to cpp (Java guy). I have 3rd party library that has method sendMail(txt).

  • 0

New to cpp (Java guy).

I have 3rd party library that has method sendMail(txt).
I don’t want to test the library. i want to test my own method, so in order to do this , i need to mock the library calls .

My own method is looking like this:

#include "mailsender.h"

int run(txt){
    analysis(txt);
    ...
    ...
    int status =  sendMail(txt);//sendMail is a 3rd party library call. i need to mock it.its not part of the unit test
    return status;
}

In Java the mailsender was interface and it was injected to my class, so in case of test i inject mock.
What is a good practice in cpp to mock library calls?
I can wrap the 3rd party library call in a class and inject this class, but i am looking for something simpler and for the common practice (maybe ifndf).

I am familiar with googlemock.
googlemock allow me to mock classes . i am not aware to option how to mock a call in my tested method.

  • 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-08T12:00:23+00:00Added an answer on June 8, 2026 at 12:00 pm

    So I assume you have a ‘global’ function that is implemented in a library that you both include a header file for (to get the definition) and link (to get the implementation).

    You obviously need to replace the implementation of the library with your own – one that does “nothing”, so you can do this in 2 ways:

    • you replace the .dll (or .so) with your own implementation that has all the methods the 3rd party library exposes. This is easy once you’ve written a new version of all the 3rd party lib functions, but writing them all out can be a pain.
    • you remove the library temporarily, and replace the calls you make to that in a .cpp source file that implements those functions. So you’d create your own sendMail() function in a .cpp file and include this into the program instead of the mailsender.h include.

    The latter is easier, but you might also have to modify your program to not link with the 3rd party lib. This can also require changing the #include as well, as some compilers (eg VC++) allow you to embed linker directives in the source. If your does this, then you won’t be able to stop the linker from including the 3rd party lib.

    The other option is to modify your code to use a different call to the sendMail call, eg test__sendMail() that you implement yourself. Wrap this is a macro to conditionally include your, or the real, function call depending on your build options.

    If this was a c++ library then you’d probably be able to use a mocking framework like you’re used to, but it sounds like its a C library, and they simply provide a list of functions that you use directly in your code. You could wrap the library in your own class and use that instead of calling the 3rd party lib functions directly.

    There is a list of C mocking frameworks.

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

Sidebar

Related Questions

I am new to NDK. I have a cpp file which has the following
I have a class that contains an array. I want this array to be
I'm new to CPP, and I want to know how to run a function
I am very new programmer to Java regular expressions. I do not want to
I'm new to C++ programming, but have been working in C and Java for
I am new to cpp (come from Java). I am writing a cpp application.
I created a new HalloWorld Makefile Project. There is a HalloWorld.cpp with my main
new on ruby and using windows xp and rails 3, i want to send
New to PHP and MySQL, have heard amazing things about this website from Leo
New to Regex. I want to validate to this format: Any character allowed, except

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.