Answer the question
In order to leave comments, you need to log in
How to test private methods for classes with complex behavior?
Hello.
TDD tells us that all private methods of a class must be tested through its public contracts. And this works well if all possible class outputs can be obtained using fairly simple queries to it (ala classic examples from TDD books about "employee name" etc.).
But when it comes to classes that implement some complex algorithm, here is the snag:
In terms of encapsulation and user friendliness, all the magic is usually hidden behind a method named like process()/compute() etc. inside which a pipeline is already called from private methods that implement the algorithm.
In this case, if you test a class through its public contract, then the test in terms of complexity may not be inferior to the algorithm itself, but to test all cases and surpass it. But, at the same time, if tests were written for individual parts of the algorithm, then they would most likely be quite simple.
And here the question arises: what is the best way to organize testing of the class functionality hidden from the user?
ps Of course, the thought of splitting a class into modules comes to mind, but then they have "fat" interfaces, because you will have to pass in the interface all the information necessary for calculations from the main class. Well, either in the test, inherit from the class under test and explicitly call private methods, which is not a beautiful solution.
z.z.y to make it clear what I'm talking about, Here is an example of a class I once wrote with a terrible design from the point of view of testing.
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question