In testing, Mocks and Stubs are really two very important conceptual confusions and there are camps on both sides. Which one to use and when? IMHO, when one is building testing infrastructure, there are two concerns in that, which needs to be taken care of.
- Test behavior
- Testing artifacts
The question of mocks or stubs relate to the testing artifacts concern and nothing to do with the Test behavior. If I draw an analogy, than let's say we want to make a software based on certain requirements. And than I have to choose a language/platform to build that software. The behaviour of my software is independent of which platform I choose (Java,.Net, PHP). And the choice of a language is not going to help me a lot in lessening my effort towards thinking process that I have to put to understand the behaviour of my software. The choice of technology will have an impact on performance, maintainability, cost,actual implementation but the behavior is not going to be impacted by that.
Let's take a concrete case to understand it better. Let's say we have a class which we want to test. One of the method that we want to test is responsible for saying whether a user, when passed to it is credit worthiness or not. The user should have a pending amount of less than 2500, to remain credit worthiness. In real application, the data of user is fetched from the database. However in testing scenario, putting a real database and than populating with right set of records is in itself a task. Our testing will be slowed down a lot. Note that in this para, we are still not concerned about stubs or mocks. We are purely concerned about the test behavior. This is a harder skill to achieve and this is the more important skill to achieve. Once you have decided your testing behavior, than comes the question of using Mocks or Stubs.
I would not show here how to write stubs or mocks as there are plenty of things on it. Regarding the choice of stub or mock, the yardstick is that if the stubbed (or mocked) object do not have a lot of different variation, go for stub otherwise go for mock. Ask the question of variation at a project level. It's better to use one strategy for the whole project, the mock way or the stub way. If my user is supposed to return the same pending amount of 2500 or smaller number of variation to it, for checking the various behavior in the system stub is a good choice. However if the user object has to return a whole lot of different values in different scenarios, mock would be better. It' what you want to achieve is more important than how you want to achieve.
No comments:
Post a Comment