Reputation: 233
My program (server) connects to a client through some middle-ware.
I made a midle-ware initialize function in my program.
init_middleware()
{
if (middle-ware can not started) // then middle-ware throw exception
return exception;
else
return success;
}
How can I unit test the init_middleware
function?
I think init_middleware
function has just one failure condition that is, when the middle-ware can not be started. But, I can't make that condition happen in a unit test.
Thank you.
Upvotes: 0
Views: 120
Reputation: 58441
It depends on what you want to test.
Test the init_middleware() function
If you want to test the init_middleware()
function, one option is to provide a Mock middle-ware
object to the function. The Mock object return the value(s) you need to completely cover the function.
Test the middle-ware object
If you want to test the middle-ware
object itself, there is really not much left to do but actually use the real object. It wouldn't be called a unit-test but an integration or even system test.
Upvotes: 3
Reputation: 22368
In strict Unit test terms you should not do this. Middleware is an external resource, just like your disk, network even databases. Since these things lie outside of the boundaries of your code (which you are testing), a real unit test should not include things like these.
Replace the middleware access code by a stub, or if you need actual feedback from it use Mocks, like Lieven proposed.
Upvotes: 0