Some times I found myself in situations in which unit tests would be easier if I change the visibility of some methods from private to package private in order to either facilitate unit test mocking, assertions...
As a simple example, say I have an object that performs several operations, one of them checking an input file and storing it in disk, normally that is the kind of method I would make as private, however, if I make it package private and the unit test for the object is in the same package, well, I could easily mock it, not only that, I could also feed it special parameters to perform stronger testing.
The drawback is of course that the visibility of the method is increased and given that such method was private for a good reason, It feels a little bit weird.
Of course I could use reflection to achieve the same, but that feels even dirtier.
So the question is, is this a good or a bad practice? is it a code smell? does it happen to someone else? is there a better technique for this?
Aucun commentaire:
Enregistrer un commentaire