Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Mock Objects in Action Paulo Caroli & Sudhindra Rao Agile 2009 © ThoughtWorks 2008."— Presentation transcript:

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

2 About us

3 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 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

17 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; }

18 Design for Testability

19

20 © 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; }

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

22

23 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"); }

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

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

26 Dependent Components

27 © ThoughtWorks 2008 Component A Component A Component B Component B Dependent Components

28 Object Graphs

29 Third Party

30 Very Slow

31 Remote Component

32 Database Component

33 Circular Dependency

34 Complex Set-up

35 Exceptional Behavior

36 Time Dependent Behavior

37 My co-worker's component

38 Mock Objects In Action

39 © ThoughtWorks 2008

40 Mock Objects in Action ArrangeActAssert

41 Mock Objects in Action Verify Set Up Exercise

42 Code Watch

43 Mock Objects Top Offenders

44 Test readability

45 Replacement for integration test

46 Unnecessary Mock (ex: simple Value Object)

47 Mock context confusion

48 Mock return a Mock

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

50 Blaming the framework

51 Mock Objects in Action Thanks for attending! Questions? Paulo Caroli & Sudhindra Rao pcaroli@thoughtworks.com srao@thoughtworks.com

52 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/


Download ppt "Mock Objects in Action Paulo Caroli & Sudhindra Rao Agile 2009 © ThoughtWorks 2008."

Similar presentations


Ads by Google