How to Mock the Internal Method of a class?

后端 未结 6 2051
别跟我提以往
别跟我提以往 2020-12-05 18:07

I have a class which has a internal method and i want to mock the internal method . But i am unable to mock it i.e. it is not calling the mocked function but calling the ori

6条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-05 18:52

    Unit testing should test the interface of a class. You can mock out dependencies, but implementation details of the class itself (such as private methods) should be tested as part of the whole class, not separately, and not changed for the test (otherwise you would be testing a different unit then would really be used).

    If you feel it is necessary to change the method to make the class testable, refactor the class so that the difficult part becomes a dependency, or otherwise substitutable by parameter or subclassing.

提交回复
热议问题