Programming 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

    Favorites, Groups & Events

    Programming With Mock Objects - Presentation Transcript

    1. Programming With Mock Objects Venkat Subramaniam venkats@agiledeveloper.com http://www.agiledeveloper.com/download.aspx Programming with Mock Objects - 1 Abstract Abstract You are convinced that Test Driven Development is good for you and your project. You realize the benefits it has to offer. What's holding you back? All the code and components that your code so heavily depends on is most likely making you wonder if TDD is really for you. We will start out by looking at dependency and dependency inversion. Then we will discuss how mock objects can help separate our code from its dependencies. In this presentation, we will take an examples oriented approach to utilizing mock objects. We will first hand toss a mock and see how our code benefits from it. Then we will take a look at using frameworks that can assist with the creation of mocks. About the Speaker Dr. Venkat Subramaniam, founder of Agile Developer, Inc., has trained and mentored thousands of software developers in the US, Canada, and Europe. He has significant experience in architecture, design, and development of software applications. Venkat helps his clients effectively apply and succeed with agile practices on their software projects, and speaks frequently at conferences. He is also an adjunct faculty at the University of Houston (where he received the 2004 CS department teaching excellence award) and teaches the professional software developer series at Rice University School of continuing studies. Venkat has been a frequent speaker at No Fluff Just Stuff Software Symposium since Summer 2002. Programming with Mock Objects - 2
    2. Programming With Mock Objects • Unit Testing… can be easy or hard • What’s a Mock? • Using Mock Objects • Mock Frameworks • To Mock or not to • Other Frameworks • Conclusion Programming with Mock Objects - 3 Unit Testing • Unit Testing has significant benefits – Serves as a design tool – Helps to make code more robust – Serves as a documentation – Safety net while refactoring – Helps quickly identify problems – Gives feedback when code begins to break –… Programming with Mock Objects - 4
    3. UT is easy when… • Unit testing is easy when – your code does not depend on any thing • No database, file system, web service, socket, … dependencies – your code can be tested fully in isolation – Does not require other classes/components, frameworks, etc. • You quickly create an object, run some tests, and off you go Code Under Test Programming with Mock Objects - 5 UT is hard when… • Unit testing is hard when – your code had dependencies – it needs to fetch that data from the database – needs access to the network for some validation – needs to validate the credit card number –… Code Under Test Programming with Mock Objects - 6
    4. But remember UT means Automation • But when we talk about unit testing, we mean automated unit testing • We need to be able to run tests quickly • Run them as many times as we like • You certainly don’t want to do either laborious and/or manual setup and teardown • You want to check for behavior and ill- behavior of dependent code as well Programming with Mock Objects - 7 Programming With Mock Objects • Unit Testing… can be easy or hard • What’s a Mock? • Using Mock Objects • Mock Frameworks • To Mock or not to • Other Frameworks • Conclusion Programming with Mock Objects - 8
    5. What’s a Mock? • A Mock object stands in for the real object • It is an object that you first coach… tell him what to say and how to dance, then you set him loose, and he mimics what you coached • Simulates the behavior of your dependencies for you Programming with Mock Objects - 9 But my code depends on real stuff Test Code Code You Test under test depend on Test Dependency Inversion Principle Test Code Interface Test under test Code Mock Test You Test provides Code with depend on Mock (or real) Programming with Mock Objects - 10
    6. What can a Mock do for you? • A Mock Object can – simulate the expected functionality – isolates your code from details that may be filled in later – speeds up development of code under test – Can simulate both behavior and ill-behavior – can be refined incrementally by replacing with actual code Programming with Mock Objects - 11 Programming With Mock Objects • Unit Testing… can be easy or hard • What’s a Mock? • Using Mock Objects • Mock Frameworks • To Mock or not to • Other Frameworks • Conclusion Programming with Mock Objects - 12
    7. A class to create • Write a Monitor class that will monitor a directory on the file system for any addition or deletion of files • It raises an alarm or sends an email when a change is detected • Remember you want to automate your tests – Each time you run your test • don’t expect me to create a file in the directory being monitored • No, don’t expect me to listen to your stinkin’ alarm or be spammed by your emails Programming with Mock Objects - 13 Let’s hand toss a mock • Mock to stand in for the code that will – Represent the file system (or change to it) – Code to raise an alarm – Code to send out an email Programming with Mock Objects - 14
    8. Programming With Mock Objects • Unit Testing… can be easy or hard • What’s a Mock? • Using Mock Objects • Mock Frameworks • To Mock or not to • Other Frameworks • Conclusion Programming with Mock Objects - 15 Frameworks to ease your pain • You can certainly hand toss your mock • In fact, that is not a bad idea – You understand what’s going on – You control your code – You have the flexibility to modify at your will • But, you may end up creating a lot of classes – Why not simply use a mock instead of creating one… this is where the frameworks come in • EasyMock, JMock, etc. Programming with Mock Objects - 16
    9. EasyMock • EasyMock helps create Mock objects for you on the fly • Instead of spending time creating a Mock class, you can get ready to use it • Let’s take a look at using EasyMock 1 Coach 3 Code 2 Mock Under Act now Behaves Test as told 4 Verify to see if calls were made as you expected Programming with Mock Objects - 17 EasyMock is Easy? • While the objective is to make it easier to create mock, the reality is it can get a bit tedious setting up the mock’s behavior • Should you use EasyMock • If it is simpler to hand toss a mock, do so • If it is easier to use EasyMock, then use it • On a project, you may use either approach at different places Programming with Mock Objects - 18
    10. Switching between Mock and Real • Your code works with Mock • Will it work with real object • When problems creep up, you may want to experiment with mock and real object • So, how can you switch between these so you can test with mock or real code • Let’s see how… Programming with Mock Objects - 19 Programming With Mock Objects • Unit Testing… can be easy or hard • What’s a Mock? • Using Mock Objects • Mock Frameworks • To Mock or not to • Other Frameworks • Conclusion Programming with Mock Objects - 20
    11. To Mock or not to? • To – Mock only code that code under test directly depends on – Mock object that is hard to work with – Mock object that takes effort to set up – Mock object whose behavior is hard to predict – Mock object that requires lots of resources – Mock objects that are computationally expensive or very slow to respond – Mock objects that your test needs to verify with • Not To – Do not mock for the sake of mocking, ask if you can eliminate the need for mock by refactoring your code – Mock your objects but not resources like database, etc. – When writing the real code is easier than writing mock – Do not mock what will slow down your UT, mocks should be used to speed up UT Programming with Mock Objects - 21 Programming With Mock Objects • Unit Testing… can be easy or hard • What’s a Mock? • Using Mock Objects • Mock Frameworks • To Mock or not to • Other Frameworks • Conclusion Programming with Mock Objects - 22
    12. Other Frameworks • You may hand toss a Mock or use EasyMock/JMock for POJOs • What about something more complex like Servlets, EJBs, JDBC, etc. • Imagine hand tossing a Mock for one of these interfaces? • You may use frameworks like MockRunner to help you with that Programming with Mock Objects - 23 MockRunner • To unit test apps in J2EE environment • Provides mock for Servlet, JDBC, JMS, struts actions, … without the real container – No need for the app server or database – Does not provide any in-container testing – Can be combined with MockEJB for EJB Programming with Mock Objects - 24
    13. Quiz Time Programming with Mock Objects - 25 Programming With Mock Objects • Unit Testing… can be easy or hard • What’s a Mock? • Using Mock Objects • Mock Frameworks • To Mock or not to • Other Frameworks • Conclusion Programming with Mock Objects - 26
    14. Conclusions • Mocks help us reap the benefit of unit testing when our code has dependencies (which is the reality) • Dependency inversion allows us to write code that can switch between mock and real code • Mocks may be hand tossed or you may use frameworks to create them • Mocks helps us pinpoint problems quickly when our code starts to break Programming with Mock Objects - 27 References 1. Pragmatic Unit Testing by Dave Thomas and Andy Hunt 2. http://www.mockobjects.com 3. http://www.easymock.org 4. http://mockrunner.sourceforge.net 5. Download examples/slides from http://www.agiledeveloper.com/download.aspx Please fill out your evaluations! Programming with Mock Objects - 28

    + Minh Chuc HoMinh Chuc Ho, 9 months ago

    custom

    303 views, 0 favs, 0 embeds more stats

    More info about this document

    © All Rights Reserved

    Go to text version

    • Total Views 303
      • 303 on SlideShare
      • 0 from embeds
    • Comments 0
    • Favorites 0
    • Downloads 5
    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