Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Mock objectsinaction paulo_caroli_and_sudhindra_rao_agile2009_final

364 views

Published on

Learn how mock objects can help testing. Keep your tests fast and specific. Thus helping debugging as well as refactoring in the long run.

Published in: Technology
  • Be the first to comment

  • Be the first to like this

Mock objectsinaction paulo_caroli_and_sudhindra_rao_agile2009_final

  1. 1. Mock Objects in Action Paulo Caroli & Sudhindra Rao Agile 2009 © ThoughtWorks 2008
  2. 2. About us
  3. 3. About us Paulo Caroli & Sudhindra Rao
  4. 4. About us Hands-on developers
  5. 5. About us Open Source
  6. 6. About us ThoughtWorks
  7. 7. About us TDD enthusiasts
  8. 8. Agenda
  9. 9. Agenda • History • Design for Testability • Dependent Components • Mock Objects in Action • Top Offenders • Q&A
  10. 10. History
  11. 11. Once upon a time we started writing Unit Tests…
  12. 12. Then we started using unit tests frameworks…
  13. 13. And soon after we started creating (and re-creating) data and implementations to help drive our tests…
  14. 14. Then we came across Mock Objects frameworks…
  15. 15. But frameworks don’t work by themselves!
  16. 16. public class Greeting { private Translator translator; private final String HELLO = "Hello"; public Greeting() { this.translator = new TranslatorImpl(); } public String sayHello(String language, String name) { String message = HELLO; // no need to call translator if the language is English if (!language.equals("English")){ message = translator.translate("English", language, HELLO ); } return message + " " + name; } }
  17. 17. Design for Testability
  18. 18. © ThoughtWorks 2008 public class Greeting { private Translator translator; private final String HELLO = "Hello"; public Greeting(Translator translator) { this.translator = translator; } public String sayHello(String language, String name) { String message = HELLO; // no need to call translator if the language is English if (!language.equals("English")){ message = translator.translate("English", language, HELLO ); } return message + " " + name; } }
  19. 19. Mock Objects Mock Objects enable your unit test to mimic behavior and verify expectations on dependent components.
  20. 20. import static org.mockito.Mockito.*; public class GreetingTest{ @Test public void shouldGreetInSomeLanguage(){ // setup Translator translator = mock(Translator.class); Greeting greeting = new Greeting(translator); when(translator.translate("English", "French”,"Hello")). thenReturn("Bonjour"); // execute assertEquals("Bonjour Madame", greeting.sayHello("French", "Madame")); // verify verify(translator).translate("English", "French", "Hello"); } }
  21. 21. Mock Objects Mock Objects enable your unit test to mimic behavior and verify expectations on dependent components.
  22. 22. Mock Objects Mock Objects enable your unit test to mimic behavior and verify expectations on dependent components.
  23. 23. Dependent Components
  24. 24. Component Component © ThoughtWorks 2008 A A Component Component B B Dependent Components
  25. 25. Object Graphs
  26. 26. Third Party
  27. 27. Very Slow
  28. 28. Remote Component
  29. 29. Database Component
  30. 30. Circular Dependency
  31. 31. Complex Set-up
  32. 32. Exceptional Behavior
  33. 33. Time Dependent Behavior
  34. 34. My co-worker's component
  35. 35. Mock Objects In Action
  36. 36. © ThoughtWorks 2008
  37. 37. Mock Objects in Action
  38. 38. Mock Objects in Action
  39. 39. Code Watch
  40. 40. Mock Objects Top Offenders
  41. 41. Test readability
  42. 42. Replacement for integration test
  43. 43. Unnecessary Mock (ex: simple Value Object)
  44. 44. Mock context confusion
  45. 45. Mock return a Mock
  46. 46. Abuse of mocks (instead: FakeDB, Dummy data, Stub, Test Fixture)
  47. 47. Blaming the framework
  48. 48. Mock Objects in Action Thanks for attending! Questions? Paulo Caroli & Sudhindra Rao pcaroli@thoughtworks.com srao@thoughtworks.com
  49. 49. Recommended links http://xunitpatterns.com/Mock%20Object.html http://developer-in-test.blogspot.com/ http://misko.hevery.com/code-reviewers-guide/ http://codebetter.com/blogs/jeremy.miller/archive/2006/01/10/136407.aspx http://martinfowler.com/articles/mocksArentStubs.html http://www.jmock.org/oopsla2004.pdf http://mocksamples.com/

×