分享

Android Service Test

 tracyf 2015-11-19
       前两篇文章对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改为所要测试的服务类名“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.             Intent intent = new Intent();

  35.             startService(intent);

  36.             MyService Serv=getService();

  37.             assertNotNull(Serv);
  38.         Log.i(TAG, "end testStart");

  39.         }

  40.     }





  41.     /**

  42.      * 测试Service正确的终止

  43.      */

  44.     public void teststop() {

  45.         Log.i(TAG, "start teststopService");

  46.             Intent intent = new Intent();

  47.             startService(intent);

  48.             MyService service = getService();

  49.             service.stopService(intent);    

  50.     }

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



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

          

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多