Skip to content
This repository was archived by the owner on Aug 29, 2023. It is now read-only.

issue.private_method

Edson Medina edited this page Jul 7, 2017 · 6 revisions

Private methods

class foo
{
    private function bar ()
    {
        // ...
    }
}

Why is this a testing issue?

Private methods are a source of headaches when it comes to testing.

You can't test them directly.

While you can test them using Reflection, this technique doesn't work if you're trying to test a private method of an abstract class.

You can't mock them

So there's no way to unit test (in isolation) methods that depend on it.

Possible refactorings

Consider 'protected' instead

Protected methods can be mocked (but not tested directly).

Refactor

Sometimes the need for a private method is just an indication that something's wrong with your design.

Clone this wiki locally