I know testing the private methods of an object is a "hot" topic. Some people say that you should not test private methods alone since they will be tested through the objects public interface. Others claim that you need to test private methods as well (the reason why PowerMock exists for example).
My own opinion is that the more I can avoid testing private methods the better. One way of avoiding it is by applying dependency injection, looking for "hidden objects" that are disguised as private methods. Then I can turn those private methods into public methods of a separate object and then hide (compose) that object inside the object that previously contained the private method calls.
After having watched the lastest clean code episode (e21), it seems Uncle Bob is saying that we should never test private methods. In case they need to be tested, the encapsulation need to be elevated. In this case I imagine that some people might say that you should not make the design of the code worse for the sake of a test (which might also go against what Uncle Bob said - Tests trump Encapsulation).
Maybe its time to look at the design of the system whenever you start thinking about testing privates? Is there even a debate? To me it feels like the nr 1 of the tests after reliability is that they conform to the OCP - that you can change production code without having to maintain test code.
Powermock: https://code.google.com/p/powermock/ (the first paragraph describes why we would need testing of privates)
BRs
Sebastian
--
The only way to go fast is to go well.
---
You received this message because you are subscribed to the Google Groups "Clean Code Discussion" group.
To unsubscribe from this group and stop receiving emails from it, send an email to clean-code-discu...@googlegroups.com.
To post to this group, send email to clean-code...@googlegroups.com.
Visit this group at http://groups.google.com/group/clean-code-discussion.
I agree with Caio, we should always test our API (intended as Application Public Interface, not as SOAP or REST etc).
Moreover we should test behaviors and not implementation... and privates and internals are usually implementations.
This video: http://vimeo.com/68375232 has been posted some topic ago and explain very well what we should test and how!
In PHP I had recently need to test a private method to verify the presence of a bug. So I'd write a "mock" class that extends the original one and witch exposes as public the target method.
The mock class is part of the test bundle, so the design of the main app as been remain unchanged.
After the test I think that is usually better to remove that mock because this is coupling my test suite on implementation in the main app.
Have fun with coding,
Gabriele