Verifying sequence of private method calls in unit testing

穿精又带淫゛_ 提交于 2019-12-31 05:02:26

问题


I have the following class:

class MyClass {

  public void doIt() {
     methodOne();
     methodTwo();
     methodThree();
  }

  private void methodOne() {
     // ...
  }

  // rest of methods similar...

}

My intention is to verify that when i invoke doIt(), methods metodOne(), methodTwo() and methodThree() will be invoked, in that order.

I'm using mockito for mocking. Does anyone knows how i can test this scenario?


回答1:


Hate to be the person but: simply don't test this. Test the output, side effects, results - not the implementation.

If you really want to ensure the correct order, extract these methods into separate class(es) and mock them.




回答2:


As long as your private methods are ... well ... private, don't test them. They are implementation details. They could be refactored without changing the contract of your class. Test the correct outcome of your public methods.



来源:https://stackoverflow.com/questions/10092926/verifying-sequence-of-private-method-calls-in-unit-testing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!