Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.

  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint

7.7. Summary

This chapter described a technique called mock objects that lets you unit test code in isolation from other domain objects and from the environment. When it comes to writing fine-grained unit tests, one of the main obstacles is to extract yourself from the executing environment. We’ve often heard the following remark: “I haven’t tested this method because it’s too difficult to simulate a real environment.” Well, not any longer!

In most cases, writing mock object tests has a nice side effect: it forces you to rewrite some of the code under test. In practice, code is often not written well. You hardcode unnecessary couplings between the classes and the environment. It’s easy to write code that’s hard to reuse in a different context, and a little nudge can have a big effect on other classes in the system (similar to the domino effect). With mock objects, you must think differently about the code and apply better design patterns, like interfaces and Inversion of Control.


  

You are currently reading a PREVIEW of this book.

                                                                                                                    

Get instant access to over $1 million worth of books and videos.

  

Start a Free Trial


  
  • Safari Books Online
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint