January 23, 2021
Hot Topics:

Getting Started with JUnit on Android

  • By Lauren Darcey & Shane Conder
  • Send Email »
  • More Articles »

Automated unit testing saves time by performing many tests for you. It can also be used as a quick way to validate new builds-in the form of build acceptance or smoke tests. Finally, unit testing can be an effective way to verify each area of functionality within your application performs as expected across a wide range of devices in a systematic and reproducible fashion. The Android SDK supports JUnit for automated unit tests. This tutorial assumes that you're familiar with Android and JUnit and are using Eclipse.

Creating a JUnit Project for Android

If you already have an Eclipse project for your Android application, that's a good place to start. If you don't already have an Eclipse project, you can create the JUnit project at the same time as you create your Android project.

For an existing Android project, right-click on the project in Eclipse, choose "Android Tools", then "New Testing Project…", as highlighted below:

Creating Android JUnit Project for Existing Project

For a new Android project in Eclipse, on the "New Android Project" dialog, press the "Next" button, highlighted below:

Adding an Android JUnit Test project while creating a new Android project

Regardless of which method you follow, you'll be able to create a new test project for your Android project at this time. The test project creation dialog, shown below, will usually fill in the appropriate details with some standard naming conventions. If not, fill them in yourself and create the project.

Editing the New Android JUnit Test Project Settings

Creating a JUnit Test Case in Android

Android applications are normally comprised of a number of Activity classes. In fact, an activity is a largely independent entity and can be tested and exercised as a cohesive unit. The Android SDK includes several classes to test your Activity classes. We'll use one now.

Right-click on your test project. Choose New, JUnit Test Case:

Creating a New Android JUnit Test Case

Now fill out the New JUnit Test Case dialog with appropriate values. The superclass to use is android.test.ActivityInstrumentTestCase2, where T is the Activity we're going to test.

Creating a New Android JUnit Test Case

Click the Finish button and this new class will be created for you. The default constructor that the wizard creates isn't right just yet. Change it so that it takes no parameters, but calls a different super() method, like so:

public ScreenValidation() { 

The setUp() method should be used to configure anything needed to run all of the associated tests in this test case. The Activity instance is available at any time by calling the getActivity() method. So, for instance, if we wanted to acquire a handle to a TextView that appears within this Activity class to use in all of our tests, we could implement the setUp() method as follows:

TextView helloText; 
 protected void setUp() throws Exception { 
   helloText = (TextView) getActivity(). 

Originally published on https://www.developer.com.

Page 1 of 2

This article was originally published on August 17, 2011

Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Thanks for your registration, follow us on our social networks to keep up-to-date