Successfully reported this slideshow.

Mockito (JUG Latvia)

2,099 views

Published on

Published in: Technology, Self Improvement
  • Be the first to comment

Mockito (JUG Latvia)

  1. 1. Mockito JUG Latvia Denis Udod
  2. 2. Mochito vs Mockito Mochito Recipe: 1/12 liter Dark Rum 3 cl Lemon Juice 1/2 handful Mint 1/12 liter Club Soda 8 drops Angostura Bitters
  3. 3. Mochito vs Mockito Mochito Recipe: 1/12 liter Dark Rum 3 cl Lemon Juice 1/2 handful Mint 1/12 liter Club Soda 8 drops Angostura Bitters Mockito Recipe: Java Unit testing Mock object
  4. 4. Mochito vs. Mockito Mochito Recipe: 1/12 liter Dark Rum 3 cl Lemon Juice 1/2 handful Mint 1/12 liter Club Soda 8 drops Angostura Bitters Mockito Recipe: Java Unit testing Mock object What is the difference?
  5. 5. Mockito: Why drink it? Q: What is unit testing? Q: What is a mock-objects? Q: Why do we mock-objects?
  6. 6. Unit testing - What's the point? Write simple test for each class method.
  7. 7. Unit testing - What's the point? Write simple test for each class method.
  8. 8. Unit testing - What's the point? Write simple test for each class method.
  9. 9. Unit testing - What's the point? Write simple test for each class method. • simple • fast • independent from "any" (database, network, filesystem)
  10. 10. Mock object - What's the point? Simulate the behavior of complex, real(non-mock) objects. • Useful when a real object is impractical or impossible to incorporate into a unit test.
  11. 11. Mock object - What's the point? Example - service object supplies non-deterministic results:
  12. 12. Mock object - What's the point? Example - service object supplies non-deterministic results:
  13. 13. Mockito: Why drink it? • Mockito is a mocking framework that tastes really well. • It lets you write beautiful tests with clean & simple API. • Mockito doesn't give you hangover.
  14. 14. Mockito: Why drink it? Syntax • Let’s keep it simple • No Strings for methods. • No anonymous inner classes. • Painless refactoring. Renaming a method should not break my tests.
  15. 15. Mockito: How to drink it? 1. Download from http://mockito.org  2. Add to project. 3. And have fun
  16. 16. Mockito: How to drink it? Create mock object:
  17. 17. Mockito: How to drink it? Create mock object:
  18. 18. Mockito: How to drink it? Create mock object:
  19. 19. Mockito: How to drink it? How about returned values?
  20. 20. Mockito: How to drink it? How about returned values?
  21. 21. Mockito: How to drink it? Check method invocations
  22. 22. Mockito: How to drink it? Check method invocations
  23. 23. Mockito: How to drink it? Check method invocations
  24. 24. Mockito: How to drink it? Check method invocations
  25. 25. Mockito: How to drink it? Check method invocations
  26. 26. Mockito: How to drink it? Spying on real object:
  27. 27. Mockito: Hangover? Limitation: • needs java 1.5+ • Cannot mock final class • Cannot mock static, final methods • Cannot mock equals(),hashCode() metods • Cannot mock private metods • Cannot verify toString()
  28. 28. Mockito Who is your bartender? Mockito is served to you by Szczepan Faber and friends. www.mockito.org

×