3

I like unit testing, it is proving its worth immensely for the last year and a half or so ive used it. However I always have a problem or rather a niggle with private methods (and protected).

I don't want to make them public or use internals visible to attribute. I want a clean and crisp solution - that is testable and i'd be proud to let someone else look at.

I am coming to the conclusion that if a private method really needs testing independantly then maybe it should be moved out onto another interface and use association to expose the functionality to the calling method. I believe this in essence to be the Facade pattern.

Is this really the best way to go about this? Or more objectively ... are there any other approaches I have totally overlooked?

Edit: Are we talking about a specific language? I am working in C#. I had kept code out of the question as i was looking for something abstract. Coming back to it today i realise that is perhaps folly due to languages really being that different.

So some code:

public class CopmlexClass
{
    public void SomeMethod()
    { }

    private void workerMethod()
    { }
}

would get re factored into

public class CopmlexClass
{
    public void SomeMethod()
    { }

    public IComplexClassWorker Worker { get; set; }

}

public interface IComplexClassWorker
{
    void WorkerMethod();        
}

In fact id probably prefer to use constructor injection and not even expose the property

My question is: is that the best way? what are the alternatives bar reflection / internals visible to attribute?

1
  • Are we talking about a specific language?
    – Neowizard
    Feb 28, 2012 at 18:43

2 Answers 2

6

A private method which needs to be tested independently can be the result of the following:

  • your class is doing too much - its public methods implement functionality which is too complex to be unit tested as a whole, and/or
  • the effects of calling the specific private method can't be directly sensed from outside the class.

Both cases are usually a clear call to extract another class containing some private method(s) of the original class, turned into public, thus made directly testable. (The sometimes challenging part is to find logically cohesive chunks of functionality which can form useful classes on their own right. You may not always get a perfect result at first - in refactoring, sometimes one needs to make a compromise, a small step into some not-yet-clearly-defined direction. In the long term, such a step may open up new possibilities, call attention to other similar code parts, or the new class may start to attract code bits from other places, eventually forming a coherent class. Or turning into something completely new, but better than what you originally envisioned.)

Exposing the private method via another interface / facade is IMO not going to solve the problem in the long term, only muddles the waters. Classes should have a well defined, complete and minimal interface. Exposing private methods in any way may open up ways to compromise the internal state of the object, which is a Bad Thing.

5
  • Ok, thanks. I have perhaps not explained what I meant very well. I meant take the private methods move them onto some new interface, implement that as a class and have the public methods call the methods on the new class which you wouldn't expose to the consumer of your main entry point. I think you are describing what I suspected was the way to do things. Thanks ... I am holding out for other answers ;) Feb 29, 2012 at 15:00
  • @JohnNicholas, thanks for the clarification, now I understand your idea. It is basically the same as the Extract Class refactoring I described above, just you insert an interface in between. IMO the interface can be omitted most of the time, unless the extracted class contains some complex internal state or has bad dependencies which make you want to mock it in unit tests. Feb 29, 2012 at 18:47
  • Well sadly, something being really evil is a prerequisite of this conversation ;) Good point about omitting the interface when you don't need to mock. Lol about thanks, your the one helping me! :D Mar 1, 2012 at 17:30
  • Do you have a preference on naming the object you just created in your extract class pattern? Mar 8, 2012 at 12:34
  • @JohnNicholas, no, to me naming is really case specific. The important thing to express is the logical relationship (if there is any) between the object/logic extracted and the source object, not the fact that historically the former used to be part of the latter. Mar 8, 2012 at 12:42
0

When we started writing unit tests in our team a couple of years ago we started with the rules you set out above - i.e. we test the public interface of an assembly.

We expected one advantage to be in detecting unreachable code. If the code coverage tools detect code blocks which not being tested, then either tests are missing or the code is unreachable and should be removed.

But in practice we haven't stuck to them. We have a very modular design - more than 30 projects in our main solution (most having a matching unit tests project). We now usually give the test project access to the internals of the project under test.

I think one problem is that we are not automatically using code coverage to detect missing tests or unreachable code. Because this is a manual process, it doesn't get done.

3
  • 1
    Sounds like your tests are actually integration tests, rather than unit tests. Not to be nitpicky, just it is good to be clear about terminology :-) Feb 28, 2012 at 16:57
  • yes - you're absolutely right! We call them unit tests because they run in a so called unit test framework, but they combine many units together
    – GarethOwen
    Feb 28, 2012 at 16:58
  • currently working in a solution with 166 projects. About 20 are test projects. We use lots of reflection to acheive the same effect s exposing internals and i don't like it ;) Feb 29, 2012 at 14:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.