So which is better. Do we start letting Tests design our code. Do we start introducing constructor injection for dependencies just to make code testable? or do we use the "override" protected method & sub class the class under test.
I generally think that testable code is good code. For code to be testable, you need better decoupling so each component can be tested in isolation with a test harness. However, there shouldn't be code in the implementation that is just used by the unit tests.
Also, keep in mind that what you need to test is the public API of an object, not it's protected/private methods. Finding bugs in private/protected method should be what logging/debuggers are for. After all, a bug in those will also propagate up to the public methods. So as long as the public methods fulfill tests, the protected methods will be covered too.
If you are using java, and have package scoped classes that implement public interfaces in the same package, I would put the unit tests in the same package in a separate folder to test those classes. You can also put unit tests in the same package as the tested class to test protected methods.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With