Easymock 101

4,185 views

Published on

A quick review of the EasyMock mocking (testing) framework and how it can make your testing more productive and less tightly coupled by Matthew McCullough.

Published in: Education
2 Comments
3 Likes
Statistics
Notes
No Downloads
Views
Total views
4,185
On SlideShare
0
From Embeds
0
Number of Embeds
10
Actions
Shares
0
Downloads
144
Comments
2
Likes
3
Embeds 0
No embeds

No notes for slide

Easymock 101

  1. 1. EasyMock 101 A 30 Minute Review by Matthew McCullough
  2. 2. ❶ What Is Mocking?
  3. 3. Are You Mocking Me? • A fake object • Allows for more modular testing • Records and verifies expectations of calls
  4. 4. ❷ Creating A Mock
  5. 5. IEnumeration<Short> iEnumIntfMock = EasyMock.createMock(EnumeratedAttribute.class);
  6. 6. ❸ Expectations
  7. 7. Expecting A Lot • Expect method is called on mock • Expect sequence of method calls on mock (call createStrictMock()) • Expect parameters to methods calls on mock
  8. 8. Expect A Call EasyMock.expect(iEnumIntfMock.getNumberOfRows() );
  9. 9. Expect A Return EasyMock.expect(iEnumIntfMock.getNumberOfRows() ).andReturn(5);
  10. 10. ❹ Replaying
  11. 11. Replay • Prime the pump to walk through expected call sequence • Provide return values in same sequence • Listen to usage of call parameters
  12. 12. Replay Kickoff EasyMock.replay(iEnumIntfMock);
  13. 13. ❺ Using The ClassUnderTest
  14. 14. Testing • Call all functions as you normally would • Pass in mock where injectable • Consider how mock can be passed into internal references
  15. 15. Testing int numOfRows = iEnumIntfMock.getNumberOfRows(); Assert.assertTrue(quot;Number of rows does not matchquot;, numOfRows > 0);
  16. 16. ❻ Verifying
  17. 17. • Called after all operations with the mock are done • Compares the usage to the recording
  18. 18. Verifying EasyMock.verify(iEnumIntfMock);
  19. 19. ❼ Resources
  20. 20. Resources • EasyMock.org • Groovy Mocking http://docs.codehaus.org/display/GROOVY/Groovy+Mocks

×