Home > Uncategorized > Play JUnit With Android

Play JUnit With Android

Getting Started with JUnit on Android

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:

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

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.

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:

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.

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() {
super("com.mamlambo.testingproject.TestingProjectActivity",
TestingProjectActivity.class);
}

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 {
super.setUp();
helloText = (TextView) getActivity().
findViewById(R.id.hello_textview);
}

Advertisements
Categories: Uncategorized
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: