Chapter 3. Testing

3.1. Introduction: Testing

Ian Darwin


“Test early and often” is a common cry among advocates of testing. As is the all-important question, “If you don’t have a test, how do you know your code works?”

There are many types of testing. Unit testing checks out individual components in isolation (not hitting the network or the database). JUnit and TestNG are the leading frameworks here. Mock objects are used where interaction with other components is required; there are several good mocking frameworks for Java.

Android provides a number of specific testing techniques, many of which are discussed here.

The terms NPE, ANR, and FC are used without further explanation in this chapter. NPE is a “traditional Java” acronym for Null Pointer Exception. ANR is an Android-specific acronym; it stands for Application Not Responding, the first few words of a dialog you get when your application is judged to be taking too long to respond to a request. FC stands for Force Close, which occurs when Android requests that you close a failed application.

3.2. Doing Test-Driven Development (TDD) in Android

Kailuo Wang


The lack of mocking support makes test-driven development in Android apps cumbersome.


Set up two test projects: one created using the Android tool for the UI-related tests, and another standard unit test project for mock supported tests. Extract as much of your logic as possible to the classes that can be unit-tested.


In the official documentation ...

Get Android Cookbook now with the O’Reilly learning platform.

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