The following is the first few sections of a chapter from The Busy Coder's Guide to Android Development, plus headings for the remaining major sections, to give you an idea about the content of the chapter.


Unit Testing

Instrumentation testing — basically, all the approaches on testing covered so far in this book — is wonderful, but it has one key problem: it is slow.

The problem stems largely from the fact that we are running our Android tests in Android. Android emulators are not speedy, and neither are devices, compared with developer machines, continuous integration servers, and the like.

Unit testing, in Android terms, is taking a subset of our tests out of Android and onto our development machine OS itself, running them just as we would plain Java tests in non-Android Java development. On the plus side, we now have much more machine power to run our tests, including the possibility of running tests in parallel across multiple CPU cores. However, whatever we are running for our development OS probably is not Android, and so attempts to use Android from our tests are doomed to failure… unless we mock Android.

In this chapter, we will explore more of why we might want to set up unit tests, the basics of setting up unit testing for plain old Java objects (POJOs), and how to use Mockito and Robolectric to mock certain things, notably Android itself.

Prerequisites

This chapter assumes that you have read the preceding chapters on testing, particularly the one covering JUnit4.

Also, the examples in this chapter are based on the Retrofit example from the chapter on Internet access, so if you skipped the Retrofit material, you may wish to go back and review that section.

I Thought We Were Already Unit Testing?

The preview of this section is unavailable right now, but if you leave your name and number at the sound of the tone, it might get back to you (BEEEEEEEEEEEEP!).

Scenario: Clean Architecture

The preview of this section was traded for a bag of magic beans.

Setting Up Unit Testing

The preview of this section left for Hollywood to appear in a reality TV show.

Writing POJO Unit Tests

The preview of this section is in an invisible, microscopic font.

Running Unit Tests

The preview of this section is presently indisposed.

Mocking Android

The preview of this section is in the process of being translated from its native Klingon.