Android 中的单元测试(使用ServiceTestCase 进行 Service测试 例子)
2012-07-29 17:43
501 查看
进行Android Service 测试之前要稍微熟悉Android Service的生命周期,onCreate只执行一次,完了后是OnStart()。对于一个已经启动的Service来说,再次调用startService()只会执行OnStart()了。
首先我们写一个最简单的Service,建立一个project 叫 AndroidService:
src/com.waitingfy.android/AndroidService.java
记得在AndroidManifest.xml中要注册这个服务
接下来我们建立一个AndroidTest的project基于上面我们刚刚建立的项目,名字叫 AndroidServiceTest
src/com.waitingfy.android.test/TestAndroidService.java
测试结果如下:
写在后面:
第一个弄的时候报了这个错误:
junit.framework.AssertionFailedError: Class com.waitingfy.android.test.TestAndroidService has no public constructor TestCase(String name) or TestCase()
是因为构造函数没有写对。
把
改成
就ok了。
源码下载: AndroidServiceTest.rar
文章源地址:http://www.waitingfy.com/?p=103
文章源地址:
首先我们写一个最简单的Service,建立一个project 叫 AndroidService:
src/com.waitingfy.android/AndroidService.java
package com.waitingfy.android; import android.app.Service; import android.content.Intent; import android.os.IBinder; import android.util.Log; public class AndroidService extends Service{ private final static String TAG = "AndroidService"; @Override public void onCreate() { super.onCreate(); Log.v(TAG, "service: onCreate()"); } @Override public void onStart(Intent intent, int startId) { super.onStart(intent, startId); Log.v(TAG, "service: onStart()"); } @Override public void onDestroy() { super.onDestroy(); Log.v(TAG, "service: onDestroy()"); } @Override public IBinder onBind(Intent intent) { // TODO Auto-generated method stub return null; } }
记得在AndroidManifest.xml中要注册这个服务
接下来我们建立一个AndroidTest的project基于上面我们刚刚建立的项目,名字叫 AndroidServiceTest
src/com.waitingfy.android.test/TestAndroidService.java
package com.waitingfy.android.test; import com.waitingfy.android.AndroidService; import android.content.Intent; import android.test.ServiceTestCase; import android.test.suitebuilder.annotation.SmallTest; public class TestAndroidService extends ServiceTestCase<AndroidService>{ public TestAndroidService() { super(AndroidService.class); } @Override protected void setUp() throws Exception { super.setUp(); getContext().startService(new Intent(getContext(), AndroidService.class)); } @SmallTest public void testSomething() { assertEquals(2, 2); } @Override protected void tearDown() throws Exception { getContext().stopService(new Intent(getContext(), AndroidService.class)); } }
测试结果如下:
写在后面:
第一个弄的时候报了这个错误:
junit.framework.AssertionFailedError: Class com.waitingfy.android.test.TestAndroidService has no public constructor TestCase(String name) or TestCase()
是因为构造函数没有写对。
把
public TestAndroidService(Class<AndroidService> serviceClass) { super(serviceClass); // TODO Auto-generated constructor stub }
改成
public TestAndroidService() { super(AndroidService.class); }
就ok了。
源码下载: AndroidServiceTest.rar
文章源地址:http://www.waitingfy.com/?p=103
文章源地址:
相关文章推荐
- 如何使用ServiceTestCase进行Android的Service类型API测试
- Android 中的单元测试(使用AndroidTestCase 进行 Content Provider 测试)
- android 之sqlite 使用及实现增、删、改、查 并使用AndroidTestCase 进行junit 测试
- Android 中的单元测试(使用 ActivityInstrumentationTestCase2 进行 Activity 测试)
- Android使用UiAutomator进行测试
- Android中使用Pull解析器解析xml文件+进行简单的单元测试
- [Android学习笔记]使用Android Junit进行测试的注意事项
- Android单元测试,使用ThreadingTest进行全新体验
- 【Android测试】在AndroidStudio中进行单元测试
- android studio的debug和release模式下使用不同的Http前缀进行测试与正式环境的自动切换
- [Android]使用自定义JUnit Rules、annotations和Resources进行单元测试(翻译)
- Android 使用Instrumentation进行界面的单元测试
- 使用Robotium进行Android Automation测试
- 使用Junit对Android应用进行单元测试
- ios测试-(一)使用XCTest进行单元测试
- 使用MonkeyTest对Android客户端进行压力测试 自动化代码
- Android中如何使用JUnit进行单元测试
- 【Android Developers Training】 108. 使用模拟定位进行测试
- AndroidTestCase_Android框架测试类使用
- AndroidTestCase来进行android 单元测试简介