Thursday, April 7, 2011

Unit Testing Tips and Tricks

Types of objects used in Unit Testing (from http://martinfowler.com/articles/mocksArentStubs.html):
Type Code Syntax
Description
Dummy dummyOperator Dummy objects are passed around but never actually used (e.g. no methods are called on it). Usually they are just used to fill parameter list.
Stubs stubOperator Provide canned answers to calls made during the test, usually not responding at all to anything outside what's programmed in for the test. Stubs may also record information about calls, such as an email gateway stub that remembers the messages it 'sent', or maybe only how many messages it 'sent'.
Mocks mockOperator Mock objects are pre-programmed with expectations which form a specification of the calls they are expected to receive.
Why using Mock Controls is better than Mock Objects
http://www.michaelminella.com/testing/mock-controls-with-easymock.html
  • Using a strict mock control (mock controls have they same three types as mock objects: regular, nice and strict) to create our mock objects means EasyMock will not only verify that the methods we record were executed, but in the correct order across objects.
  • Instead of having to specify all the mock objects to replay and verify, our mock control kept track of what was recorded where so all we have to do is tell it to replay and verify.