前两篇文章对Android Service和ServiceTestCase做了简单的分析,在本文中将一步步实现对一个Service的测试,由于参考的资料非常有限,大部分都是自己研究摸索的,不保证正确性。在以后的工作中,我会进行进一步的研究。

       首先做一下对服务的启动和停止的测试。测试的对象是一个很简单的播放音乐的服务,代码是我在网上搜的,对其做了一些修改来方便测试,具体代码如下:



package com.example;


import android.app.Service;

import android.content.Intent;

import android.media.MediaPlayer;

import android.os.IBinder;

import android.util.Log;

import android.widget.Toast;


public class MyService extends Service {


    MediaPlayer player;


    public IBinder onBind(Intent intent){

        return null;

    }


    public void onCreate() {

        Toast.makeText(this, "My Service Created", Toast.LENGTH_LONG).show();

        player=MediaPlayer.create(this, R.raw.start);

        player.setLooping(false);

    }


    public void onDestroy(){

        Log.d("stop","stoped");

        Toast.makeText(this, "My Servece Stopped", Toast.LENGTH_LONG).show();

        player.stop();

    }


    public void onStart(Intent intent,int startid){

        Toast.makeText(this, "Started", Toast.LENGTH_LONG).show();

        player.start();

    }

}


     可以看到该服务非常的简单,我们对其的测试相对应地也很简单。下面就一步步进行测试。


1.在ECLIPSE中运行File>New > Project > Android > Android Test Project.如下图所示,输入Test Project Name并且在“Test Tartget”中选择所要测试的工程。其余的会自动填好。最后点击“Finish”按钮。





2.在新建的项目上右击鼠标,选择NEW>CLASS。如下图所示,输入类名。在Superclass一览点击Browse,选择“android.test.ServiceTestCase<T>”,将其中的T改为所要测试的服务类名“MyService”。点击finish按钮。这样第一个测试类就创建了。





3.在新建的类中输入代码:



  1. package com.example.test;

  2. import com.example.MyService;
  3. import android.content.Intent;
  4. import android.test.ServiceTestCase;
  5. import android.util.Log;

  6. public class MyServiceTest extends ServiceTestCase<MyService> {

  7.     private String TAG="myservicetest";
  8.     private Context mContext;
  9.     /**
  10.      * 构造方法
  11.      */
  12.     public MyServiceTest() {
  13.         super(MyService.class);

  14.     }

  15.     /**
  16.      * 重写setUp方法,第一句调用super.setUp
  17.      */
  18.     protected void setUp() throws Exception {
  19.         super.setUp();
  20.         mContext = getContext();

  21.     }

  22.   // public void testAndroidTestCaseSetupProperly() {
  23.   // super.testAndroidTestCaseSetupProperly();
  24.  // }

  25.     protected void tearDown() throws Exception {
  26.         mContext = null;
  27.         super.tearDown();
  28.     }

  29.     /**
  30.      * 测试Service正确地启动
  31.      */
  32.     public void testStart() {
  33.         Log.i(TAG, "start testStart");
  34.         try {
  35.             Intent intent = new Intent();
  36.             startService(intent);
  37.             MyService Serv=getService();
  38.             assertNotNull(Serv);

  39.          } catch (Exception e) {
  40.             Log.e(TAG, e.getMessage());
  41.             e.printStackTrace();
  42.             fail(e.getMessage());
  43.         } finally {
  44.             Log.i(TAG, "end testStart");
  45.         }
  46.     }


  47.     /**
  48.      * 测试Service正确的终止
  49.      */
  50.     public void teststop() {
  51.         Log.i(TAG, "start teststopService");

  52.         try {
  53.             Intent intent = new Intent();

  54.             startService(intent);
  55.             MyService service = getService();

  56.             service.stopService(intent);
  57.         }
  58.         catch (Exception e) {
  59.             e.printStackTrace();
  60.             fail(e.toString());
  61.         }
  62.         finally {
  63.             Log.i(TAG, "end teststopService");
  64.         }
  65.     }
  66. }


4.在工程上右击鼠标选择Run As> Android JUnit Test运行测试用例,测试结果如下图所示,可以看到测试都通过了,如果测试没通过,在下面的“Failure Trace”中会给出错误信息。最后的两个测试用例是系统自动运行的。



     至此,第一个测试例子就结束了,可以看到这个例子非常地简单,在实际开发中所要用的肯定比这复杂得多,还需要对其进行更深入的研究,比如说加入mock object 等。