I would like to test a method in my program, which handles shutdown of the application. At the end this method calls exit(0);
As expected this shuts also my test application down.
Is there a possibility to catch the exit signal in this particular unit test, so the shutdown of the test application can be avoided?
Since there is no way to prevent exit() from ending the program, you will have to change the legacy application in some way or another.
For a similar problem I used the following solution:
static void Wrapper::exit( int exit_code);
virtual void Base::exit( int exit_code) = 0;
Base
, that implements the normal behaviour:void OsImpl::exit( int exit_code) { ::exit( exit_code); }
Wrapper
class finally contains a pointer to the implementation to use, by default an object of OsImpl
, but that can be replaced by e.g. a TestImpl
that does nothing.static void Wrapper::setImpl( Base* handler);
::exit( 0);
by Wrapper::exit( 0);
I know this is quite condensed, but I hope you get the idea.