Mocking every field or good api
How do you write code that easy to unittesting and encapsulates data at the same time?
For example, if I want unittest some class, I declare getters and setters for all fields. Otherwise I can't inject mocks. On the other hand I want to wr开发者_StackOverflowite class that have a really good api with really needed public methods.
I believe you understand me.
If your class has a collaborator that needs mocking, then it will get it from somewhere - either through the constructor, or through a setter. So having those is not a design problem.
But I wouldn't have getters for everything, unless they are part of the API naturally. You should be testing the observed behaviours of the object under test - and that means what you can naturally get to via its public API.
I once worked on a project where a developer insisted on making everything public so that he could test the internal state. That was... ummm... fun.
精彩评论