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
Share this Page URL
Help

Chapter 13. Testing and instrumentation > Beyond instrumentation: mocks and mon...

13.3. Beyond instrumentation: mocks and monkeys

In the first two parts of this chapter, we showed you what makes Android tests roll, from setting up a test project to writing both simple and more complex test cases. We’re not quite done yet. This last section will deal with the advanced themes of testing on Android, going beyond your typical instrumentation tests. We’ll start with covering mock objects and explain why and how you should use them in tests. We’ll then leave the world of Android JUnit tests and explore some alternate techniques of testing your applications that fundamentally differ from what we’ve seen so far, but that can be used complementary to your ordinary Android test cases.

Technique 78: Mock objects and how to use them

There’s a golden rule when writing tests: never let the outcome of a test depend on something that’s not directly related to the entity under test, or worse, that’s beyond your control. We saw this rule in practice when we wrote a unit test for the DealDetails activity in technique 68, where the entire test environment in which the Activity was executed acted as a barrier. The test couldn’t possibly have failed due to the web browser crashing when we tested the “view in browser” functionality, since no actual web browser process was running! We merely tested an if-then scenario: if that menu item was pressed on a real device, then the browser would be started. That means we tested this piece of functionality without having to actually rely on the browser application, which is beyond our control. This is desirable, since we don’t actually care whether the browser works; we only care that if the browser works, then our application should work, too.


  

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