Chapter    4

Mocking

One of the major goals whether it’s on the Android platform or not is to isolate the code that we’re testing. When we write our tests, we want to test a specific class’s method without any of the associated interactions with other classes in the app or any external elements, such as a web service. We should be testing a single method, not its dependencies and this method should be the only code covered by the test with everything else mocked.

Mocking out these third-party interactions is a great way to help us put a fence around a method so we’re not reliant on such things as the network or a device’s location or US or UK time when we’re doing our testing. The only reason a test should fail is because there’s something wrong ...

Get Agile Android now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.