Testing – With Mock Objects

Loading...

Flash Player 9 (or above) is needed to view presentations.
We have detected that you do not have it on your computer. To install it, go here.

0 comments

Post a comment

    Post a comment
    Embed Video
    Edit your comment Cancel

    1 Favorite

    Testing – With Mock Objects - Presentation Transcript

    1. Testing – with Mock objects Emmett Walsh
    2. A common problem • Writing a test for a DAO manager – End up writing same test as that for the DAO(s) – Pre-populate DB before tests – Verify DB during/after tests • Not true unit test – more an integration test • If DAO fails , the manager test fails • Would be nice not to have to rely on DB
    3. A Solution • Allow dummy/mock DAO (or other classes) be injected into class under test • Provide setters for objects we would like to mock
    4. Creating Mock objects • Create mock impl of interface that returns ‘fake’ results (i.e. no db access involved) • Or subclass current implementation and override methods • Example…
    5. private class MockFormulaDAO implements IFormulaDAO { public int create(Formula formula, ORDatabaseTransaction transaction) throws DatabaseException { if (formula != null){ //return canned response return 1; } else { throw new NullPointerException(); } } . . . Etc Etc.
    6. Disadvantages • Time consuming • Pollutes code base • More maintenance (e.g. if interface changes) • Much effort involved to add more sophisticated features (e.g. param checking, call order)
    7. Mock frameworks • Mockito • JMock • JMockit (allows mocking of static methods) • Powermock (allows mocking of static methods) • EasyMock
    8. Advantages • Remove the grunt work • Allows to easily simulate collaborators in a test • Offers more features – Order checking of calls to mock object – Param checking in calls to mock object – Can run in various modes (nice v strict) – Mature (Easymock 4yrs, at v2.5.1, lots docs)
    9. EasyMock • Generates mock objects on the fly from interfaces or classes (uses java.lang.reflect.Proxy) • Uses a 4 stage lifecycle – Create mock(s) – Set expectations on the mocks – Put mock(s) in ready mode – Verification of expectations (i.e. the expected methods got called on the mock(s) )
    10. Demo • FormulaManagerTest.java (currently uses hand made mocks)
    11. Results • Code coverage before EasyMock – Block 64% – Line 64% • Code coverage after EasyMock – Block 100% – Line 100%
    12. EasyMock - Conclusion • Helps us write better tests in shorter time • Allows us to possible separate unit tests from integration tests – they run fast! • True unit tests could possibly be run at customer site as a quick sanity check ??? • Easy – low learning curve • Reliable – comes with its own unit tests, 180+ test classes
    13. End
    SlideShare Zeitgeist 2009

    + emmettwalshemmettwalsh Nominate

    custom

    330 views, 1 favs, 0 embeds more stats

    Quick slideshow on using mock objects to test Java more

    More info about this document

    © All Rights Reserved

    Go to text version

    • Total Views 330
      • 330 on SlideShare
      • 0 from embeds
    • Comments 0
    • Favorites 1
    • Downloads 9
    Most viewed embeds

    more

    All embeds

    less

    Flagged as inappropriate Flag as inappropriate
    Flag as inappropriate

    Select your reason for flagging this presentation as inappropriate. If needed, use the feedback form to let us know more details.

    Cancel
    File a copyright complaint
    Having problems? Go to our helpdesk?

    Categories