1 / 52

Mock Objects in Action

Mock Objects in Action. Paulo Caroli & Sudhindra Rao Agile 2009. About us. About us. Paulo Caroli & Sudhindra Rao. About us. Hands-on developers. About us. Open Source. About us. ThoughtWorks. About us. TDD enthusiasts. Agenda. Agenda. History Design for Testability

adolph
Download Presentation

Mock Objects in Action

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Mock Objects in Action Paulo Caroli & Sudhindra Rao Agile 2009 © ThoughtWorks 2008

  2. About us

  3. About us Paulo Caroli & Sudhindra Rao

  4. About us Hands-on developers

  5. About us Open Source

  6. About us ThoughtWorks

  7. About us TDD enthusiasts

  8. Agenda

  9. Agenda • History • Design for Testability • Dependent Components • Mock Objects in Action • Top Offenders • Q&A

  10. History

  11. Once upon a time we started writing Unit Tests…

  12. Then we started using unit tests frameworks…

  13. And soon after we started creating (and re-creating) data and implementations to help drive our tests…

  14. Then we came across Mock Objects frameworks…

  15. But frameworks don’t work by themselves!

  16. publicclass Greeting { private Translator translator; privatefinalString 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. Design for Testability

  18. publicclass Greeting { private Translator translator; privatefinalString 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; } } © ThoughtWorks 2008

  19. Mock Objects Mock Objects enable your unit test to mimic behavior and verify expectations on dependent components.

  20.                   import static org.mockito.Mockito.*; publicclass GreetingTest{                         @Test publicvoid 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. Mock Objects Mock Objects enable your unit test to mimic behavior and verify expectations on dependent components.

  22. Mock Objects Mock Objects enable your unit test to mimic behavior and verify expectations on dependent components.

  23. Dependent Components

  24. Dependent Components Component A Component B © ThoughtWorks 2008

  25. Object Graphs

  26. Third Party

  27. Very Slow

  28. Remote Component

  29. Database Component

  30. Circular Dependency

  31. Complex Set-up

  32. Exceptional Behavior

  33. Time Dependent Behavior

  34. My co-worker's component

  35. Mock Objects In Action

  36. © ThoughtWorks 2008

  37. Mock Objects in Action

  38. Mock Objects in Action

  39. Code Watch

  40. Mock Objects Top Offenders

  41. Test readability

  42. Replacement for integration test

  43. Unnecessary Mock (ex: simple Value Object)

  44. Mock context confusion

  45. Mock return a Mock

  46. Abuse of mocks (instead: FakeDB, Dummy data, Stub, Test Fixture)

  47. Blaming the framework

More Related