Stubs和Mocks区别 (Stubs vs. Mocks)
翻译自《Programming Groovy － Dynamic Productivity for the jdk Developer》的P243。
Stubs vs. Mocks
In the article “Mocks Aren’t Stubs,” (http://martinfowler.com/articles/mocksArentStubs.html ), Martin Fowler discusses the difference between stubs and mocks. A stub stands in for a real object. It simply reciprocates the coached expected response when called by the code being tested. The response is set up to satisfy the needs for the test to pass. A mock object does a lot more than a stub. It helps you ensure your code is interacting with its dependencies, the collaborators, as expected. It can keep track of the sequence and number of calls your code makes on the collaborator it stands in for. It ensures proper parameters are passed in to method calls. While stubs verify state, mocks verify behavior. When you use a mock in your test, it veriﬁes not only the state but also the behavior of the interaction of your code with its dependencies. Groovy provides support for creating both stubs and mocks, as you will see in Section 16.10, Mocking Using the Groovy Mock Library, on page 254.
顺便推荐一下《Programming Groovy － Dynamic Productivity for the jdk Developer》这本书。
这是一本Groovy进阶的书，入门可以先看一下《Groovy Recipes －Greasing the Wheels of jdk 》或是《Groovy Programming －An Introduction for jdk Developers》。Groovy的资料可以看看：http://blog.csdn.net/oldrat/archive/2010/02/22/5317366.aspx