I often find myself changing my code to make it more testable, I always wonder whether this is a good idea or not. Some of the things I find myself doing are:
- Adding setters just so I can set an internal object to a mock.
- Adding getters for internal maps/lists so I can check the internal state of the object has changed after performing some external action.
- Wrapping concrete system classes and creating a new interface so I can mock them. For example, File classes can be hard to mock – so I’ll create a new interface FileInterface and WrappedFile which extends it and then use the FileInterface instead of File.
Changing your code to make it more testable can be a good thing, but only if it makes your code itself better. Refactoring for testability can make your code better independent of the test suite’s needs. Those are good changes.
Of your three examples only #3 is a really good one; often those new interfaces will make your code more flexible for regular use later. #1 is usually addressed for testing via dependency injection, which in my mind makes code needlessly more complicated but does at least make it easier to test. #2 sounds like a bad idea in general.