O'Reilly logo

Android Cookbook by Ian F. Darwin

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Chapter 3. Testing

3.1. Introduction: Testing

Ian Darwin

Discussion

“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

Problem

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

Solution

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.

Discussion

In the official documentation ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required