在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

鍍金池/ 教程/ Android/ Android 測(cè)試教程(12):ServiceTestCase 示例
monkeyrunner 簡(jiǎn)介
Android 測(cè)試教程(14):ActivityInstrumentationTestCase2 示例
ApplicationTestCase 示例
Android 測(cè)試教程(13):TestCase 示例
ActivityInstrumentationTestCase2 示例
測(cè)試 Activity
測(cè)試項(xiàng)目
Android 測(cè)試教程(12):ServiceTestCase 示例
測(cè)試相關(guān) API
Monkey 命令行工具
第一個(gè)測(cè)試項(xiàng)目 HelloWorldTest
測(cè)試 Content Provider
Android 測(cè)試教程(11):ActivityUnitTestCase 示例
測(cè)試基礎(chǔ)
AndroidTestCase 示例
測(cè)試 Service

Android 測(cè)試教程(12):ServiceTestCase 示例

ServiceTestCase 為測(cè)試 Service 提供了一個(gè)可控的測(cè)試環(huán)境,它提供對(duì) Service 生命周期的基本支持,並可以通過注入一些依賴對(duì)象來控制測(cè)試環(huán)境以便測(cè)試 Service。

ServiceTestCase 的類繼承如下圖所示:

http://wiki.jikexueyuan.com/project/android-test-course/images/12.1.jpg" alt="picture12.1" />

Service Lifecycle 支持, 每個(gè) Service 運(yùn)行 都遵循一定的順序(生命周期方法),ServiceTestCase 提供下面方法來支持對(duì) Service 生命周期方法的測(cè)試:

  • 每個(gè)測(cè)試方法調(diào)用之前首先會(huì)執(zhí)行 setUp 方法,setUp 的基本實(shí)現(xiàn)是取得系統(tǒng) Context ,如果你要重載 setUp 的話,注意在第一行加上 super.setUp.
  • 在調(diào)用 startService(Intent) 或 bindService(Intent) 之後,ServiceTestCase 才會(huì)調(diào)用 Service 的 onCreate 方法,從而使你有機(jī)會(huì)在 Service 啟動(dòng)之前對(duì)測(cè)試環(huán)境做些調(diào)整。
  • 當(dāng)你的測(cè)試方法調(diào)用 startService(Intent) 或 bindService(Intent) 之後,ServiceTestCase 調(diào)用 Service 的 onCreate 方法,然後再調(diào)用 Service 相應(yīng)的 startService(Intent) 或 service 的bindService(Intent, ServiceConnection, int)方法。並保存用於 tracking 和支持 Lifecycle 對(duì)應(yīng)的值。
  • 每個(gè)測(cè)試方法結(jié)束後,調(diào)用 tearDown 方法,這個(gè)方法 stop 並destroy 被測(cè)試的 service. 如果你需要重載 tearDown, 注意先調(diào)用 super.tearDown.

Dependency Injection 每個(gè) Service 都依賴於運(yùn)行它的 Context 對(duì)象和 Application 對(duì)象,ServiceTestCase 測(cè)試框架允許你注入這些對(duì)象(修改過,Mocked 等)以實(shí)現(xiàn)真正的單元測(cè)試.

LocalServiceTest 的代碼如下:


    public class LocalServiceTest
     extends ServiceTestCase<LocalService> {

     public LocalServiceTest() {
     super(LocalService.class);
     }

     @Override
     protected void setUp() throws Exception {
     super.setUp();
     }

     @SmallTest
     public void testPreconditions() {
     }

     /**
     * Test basic startup/shutdown of Service
     */
     @SmallTest
     public void testStartable() {
     Intent startIntent = new Intent();
     startIntent.setClass(getContext(), LocalService.class);
     startService(startIntent);
     }

     /**
     * Test binding to service
     */
     @MediumTest
     public void testBindable() {
     Intent startIntent = new Intent();
     startIntent.setClass(getContext(), LocalService.class);
     IBinder service = bindService(startIntent);
     }

    }

testStartable 測(cè)試對(duì)應(yīng)的 Service 能否正常啟動(dòng)。

testBindable 測(cè)試對(duì)應(yīng)的 Service 能否綁定成功

Tags: Android 測(cè)試