Davids
Davids

Reputation: 9

How to testing public methods when they have private methods dependency

How to test public methods when they have private methods dependency

Example:

Class Person {
    private String name;

    public void getFancyname(String name){
        generateName();
    }

    private generateName(){ ... }

Upvotes: 0

Views: 75

Answers (1)

Maciej Kowalski
Maciej Kowalski

Reputation: 26502

Private methods are an implementation detail of a public method.

They should never be mocked/stubbed etc. (only with the exception of some complex legacy code).

If your public method is using a lot of them you might think about extracting some of the logic contained in the private methods into a separate class and test that logic in isolation. This is called the Sprout Method. You would reduce the complexity of the public method itself and allow it to be more easily tested

Upvotes: 1

Related Questions