开发者

How to unit test machine control function or middleware control function

开发者 https://www.devze.com 2023-01-25 11:39 出处:网络
My program (server) connects to a client through some middle-ware. I made a midle-ware initialize function in my program.

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 开发者_StackOverflow社区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.


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号