Mock with Mockito

1,849 views

Published on

Published in: Technology, Business
0 Comments
6 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
1,849
On SlideShare
0
From Embeds
0
Number of Embeds
20
Actions
Shares
0
Downloads
0
Comments
0
Likes
6
Embeds 0
No embeds

No notes for slide

Mock with Mockito

  1. 1. Mocks with Mockito API Camilo Lopes www.camilolopes.com
  2. 2. Agenda• About Mocks• Mocks API• Mockito API• Mocks with Mockito• Conclusion
  3. 3. What Mock is?
  4. 4. In one setence“Mocks are object that simulates the behavior of real an object.”
  5. 5. Why Mocks?• Help design solution;• Avoid high coupling;• Promove high cohesion;• Easy to use;• Promove Agile development;• Improve performance in unit test;
  6. 6. Where can I use Mock?• DataBase;• WebServices;• NetWork• Classes;
  7. 7. How to use Mock?
  8. 8. Mocks API
  9. 9. What is the best?
  10. 10. In my opinion is...
  11. 11. Why?
  12. 12. Because...
  13. 13. Mockito API• Easy to use;• High learning Curve;• Excellent Documentation;• “stub-run-verify” concept;• Concept stub is separed of verification;• Clear code;
  14. 14. And the others?• Use “except-run-verify” concept; (it is not good)• Code is not as clear as Mockito;• Documentation is not as good as Mockito ‘s doc;• Verify and expect are bult-in in expect() method; (Mockito is separated. It is better)
  15. 15. Mockito Features Examples ahead
  16. 16. Exceptionwhen(myobjectMock.buy(new BigDecimal("-5000.00"))).thenThrow(new Exception());
  17. 17. Matcherswhen(myobjectMock.buy(any(BigDecimal.class)).thenReturn(true); NoteMockito offers some built-in such as: anyString, anyInt(), any(class). But we can create customized built-in. Our class must extends ArgumentMatcher.
  18. 18. void method• doAnswer, doNothing, doThrow or doReturndoThrow(new Exception()).when(mockObject).buy(new BigDecimal("-5000"))
  19. 19. Verify it possible check the number invocation of method• atLeastOnce(), atLeast(int), atMost(int), never()verify(mockobject,times(3)).buy(new BigDecimal("10000"))
  20. 20. More featuresCheck Mockito Documentation. There are many examples.http://docs.mockito.googlecode.com/hg/org/mockito/ Mockito.html
  21. 21. ConclusionIf you want to improve of quality in unit test use Mock.It will contribute to design, code, development etc.There are many APIs in market, but the most used are: EasyMock, Jmock and Mockito.
  22. 22. Thanks!!!!
  23. 23. Referenceshttp://code.google.com/p/mockito/http://www.infoq.com/news/2008/06/mocks-q-of-whenhttp://www.easymock.org/http://martinfowler.com/articles/mocksArentStubs.html

×