Mocks Are Not Stubs

Mocks and stubs are very similar but there are two very big differences, stub use state verification while mocks use behavior verification.

In order to use state verification on the stub, I need to make some extra methods on the stub to help with verification. As a result the stub implements MailService but adds extra test methods.

Mock objects always use behavior verification, a stub can go either way. Meszaros refers to stubs that use behavior verification as a Test Spy. The difference is in how exactly the double runs and verifies and I’ll leave that for you to explore on your own.

Mocks can be much more useful in certain situtations because they are not real objects in the testing.

http://martinfowler.com/articles/mocksArentStubs.html

Leave a comment