• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Mock objects vs stubs

 
Ranch Hand
Posts: 151
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I would like to know what is the difference between a mock object and a stub?
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
http://martinfowler.com/articles/mocksArentStubs.html#TheDifferenceBetweenMocksAndStubs
 
rakesh sugirtharaj
Ranch Hand
Posts: 151
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I went through that article but i find it difficult to understand the code examples and also what he means by behavior here?
 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Stubs and mocks, indeed, have a lot in common. They are not the "real" objects, and they can record information about what has been done with them.

According to Martin Fowler (see link above), the basic difference between stubs and mocks is that stubs use state verification and mocks use behavior verification.

In practice, if you write a class that implements the same interface as the "real" objects and provide some mechanisms for recording the state (e.g. how many times some method has been called), this is a stub. With mocks, in contrast, *every* method call is checked, and there is not a specific mechanism for recording some special events, rather expectations about *everything* (possibly with some exception) are set up and compared with what has really been called. Usually, it does not make sense, to write mocks from the ground up; rather, you should use a framework like EasyMock. If, however, you really implemented mocks from the ground up, in contrast to stubs, this would imply a heavy use of reflection.
reply
    Bookmark Topic Watch Topic
  • New Topic