Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1535194
  • 博文数量: 113
  • 博客积分: 3526
  • 博客等级: 中校
  • 技术积分: 1815
  • 用 户 组: 普通用户
  • 注册时间: 2009-09-08 09:46
个人简介

记录总结自己的工作

文章分类

全部博文(113)

文章存档

2015年(19)

2014年(10)

2013年(6)

2012年(16)

2011年(24)

2010年(21)

2009年(17)

分类: 嵌入式

2011-02-21 09:23:46

       前两篇文章对
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();
    }
}



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 等。

阅读(3830) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~