Testing ItemSelectedListener for Spinner using Android Testing Framework

November 18, 2012

In this article, we will create a test application in Android Testing Framework, to test itemselectedlistener for a Spinner widget. The application developed in the article titled “ItemSelectedListener for Android Spinner Widget – Example” is tested in this application.

This test application is developed in Eclipse (4.2.1) with ADT plugin (20.0.3) and Android SDK (R20.0.3).

1. What to Test

In this test application, we will test whether the item selected in the Spinner widget is correctly displayed in the TextView widget

2. Download the application SpinnerWidgetItemSelectedListener from the given below link

3. Extract the downloaded file and import into Eclipse IDE

4. Create an Android Test Project with the given below details

Project Name : SpinnerWidgetItemSelectedListenerTest

Location : Default Location

Build Target : Android 1.6

Test Target : SpinnerWidgetItemSelectedListener (The application to be tested )

5. Create a TestCase class namely MainActivityTest with the given below code in the file src/in/wptrafficanalyzer/spinnerwidgetitemselectedlistener/test/MainActivityTest.java

package in.wptrafficanalyzer.spinnerwidgetitemselectedlistener.test;

import in.wptrafficanalyzer.spinnerwidgetitemselectedlistener.MainActivity;
import android.test.ActivityInstrumentationTestCase2;
import android.test.TouchUtils;
import android.test.suitebuilder.annotation.SmallTest;
import android.view.KeyEvent;
import android.widget.Spinner;
import android.widget.TextView;

public class MainActivityTest extends ActivityInstrumentationTestCase2 {

    /** The activity containing spinner widget */
    MainActivity mActivity;

    /** The Spinner widget to be tested */
    Spinner mSpinner;

    /** The selected item is displayed in this TextView */
    TextView mTvCountry;
    public MainActivityTest() {

    protected void setUp() throws Exception {

        /** Getting a reference to the MainActivity of the target application */
        mActivity = getActivity();

        /** Getting a reference to the Spinner widget */
        mSpinner = (Spinner) mActivity.findViewById(in.wptrafficanalyzer.spinnerwidgetitemselectedlistener.R.id.spr_country_list);

        /** Getting a reference to the TextView widget */
        mTvCountry = (TextView) mActivity.findViewById(in.wptrafficanalyzer.spinnerwidgetitemselectedlistener.R.id.tv_country);

    protected void tearDown() throws Exception {

    public void testSpinner(){
        final int INITIAL_POSITION=0;
        final int FINAL_POSITION=mSpinner.getAdapter().getCount();
        String expected = "";
        String actual = "";

        /** Iterates through each item in the Spinner widget */
        for(int i=INITIAL_POSITION;i<=FINAL_POSITION;i++){
            try {
                runTestOnUiThread(new Runnable() {
                    public void run() {
                        /** Sets the first item as the current item in the Spinner widget */
            }catch (Throwable e) {

            /** Taps the Spinner Widget */
            TouchUtils.tapView(this, mSpinner);

            /** Send i number of Down KeyPad event to Spinner widget,
            * since it is the currently selected widget */

            /** Sets the currently selected item as the current item of the Spinner */

            /** Getting the currently selected item */
            expected = mSpinner.getSelectedItem().toString();

            /** Getting the text displayed in the TextView widget */
            actual = mTvCountry.getText().toString();

            /** Checks, whether currently selected Spinner item and the text displayed are same
            * If same, then the test is success, otherwise failed*/
            assertEquals(expected, actual);

            /** This Thread.sleep() is not necessary, but provided just to slow down the testing process
            * so that we can manually monitor the happenings while testing in progress
            try {
            } catch (InterruptedException e) {

6. Screenshots of the application

Automated Testing in Progress

Figure 1 : Automated Testing in Progress


Showing results after automated testing

Figure 2 : Showing results after automated testing

7. Download

8. Reference


How to hire me?

I am George Mathew, working as software architect and Android app developer at wptrafficanalyzer.in

You can hire me on hourly basis or on project basis for Android applications development.

For hiring me, please mail your requirements to info@wptrafficanalyzer.in.

My other blogs

Android Knowledge Quiz

Ready to test your knowledge in Android? Take this quiz :

Tags: , , , , , ,

Leave a Reply

Your email address will not be published. Required fields are marked *

Be friend at g+

Subscribe for Lastest Updates

FBFPowered by ®Google Feedburner