サービスをテストする

アプリのコンポーネントとしてローカル Service を実装する場合は、Service をテストして、予期しない動作が発生しないことを確認する必要があります。Service の動作が正しいかどうかを検証するには、インストゥルメント化単体テストを作成します。たとえば、サービスが有効なデータ値を格納して返し、データ処理を正しく実行することを確認します。

AndroidX Test は、隔離された状態で Service オブジェクトをテストするための API を提供します。ServiceTestRule クラスは、単体テストメソッドが実行される前にサービスを開始し、テストの完了後にサービスをシャットダウンする JUnit 4 ルールです。このテストルールを使用することで、テストメソッドが実行される前に、確実にサービスへの接続を確立できます。JUnit 4 ルールの詳細については、JUnit のドキュメントをご覧ください。

: ServiceTestRule クラスは、IntentService オブジェクトのテストをサポートしていません。IntentService オブジェクトをテストする必要がある場合は、ロジックを別のクラスにカプセル化して、対応する単体テストを作成する必要があります。

テスト環境をセットアップする

サービスの統合テストを作成する前に、AndroidX Test 用にプロジェクトをセットアップするの説明に従って、インストゥルメント化テスト用にプロジェクトを構成してください。

サービスの統合テストを作成する

統合テストは、JUnit 4 テストクラスとして作成する必要があります。JUnit 4 テストクラスの作成方法と JUnit 4 アサーション メソッドの使用方法の詳細については、インストゥルメント化単体テストクラスを作成するをご覧ください。

サービスの統合テストを作成するには、テストクラス定義の先頭に @RunWith(AndroidJUnit4::class) アノテーションを追加します。また、AndroidX Test でデフォルトのテストランナーとして提供されている AndroidJUnitRunner クラスを指定する必要があります。この手順の詳細については、インストゥルメント化単体テストを実行するをご覧ください。

次に、@Rule アノテーションを使用して、テスト内に ServiceTestRule インスタンスを作成します。

Kotlin

    @get:Rule
    val serviceRule = ServiceTestRule()
    

Java

    @Rule
    public final ServiceTestRule serviceRule = new ServiceTestRule();
    

次の例は、サービスの統合テストの実装方法を示しています。 テストメソッド testWithBoundService は、アプリがローカル サービスに正常にバインドされ、サービス インターフェースが正しく動作するかどうかを検証します。

Kotlin

    @Test
    @Throws(TimeoutException::class)
    fun testWithBoundService() {
        // Create the service Intent.
        val serviceIntent = Intent(
                ApplicationProvider.getApplicationContext<Context>(),
                LocalService::class.java
        ).apply {
            // Data can be passed to the service via the Intent.
            putExtra(SEED_KEY, 42L)
        }

        // Bind the service and grab a reference to the binder.
        val binder: IBinder = serviceRule.bindService(serviceIntent)

        // Get the reference to the service, or you can call
        // public methods on the binder directly.
        val service: LocalService = (binder as LocalService.LocalBinder).getService()

        // Verify that the service is working correctly.
        assertThat(service.getRandomInt(), `is`(any(Int::class.java)))
    }
    

Java

    @Test
    public void testWithBoundService() throws TimeoutException {
        // Create the service Intent.
        Intent serviceIntent =
                new Intent(ApplicationProvider.getApplicationContext(),
                    LocalService.class);

        // Data can be passed to the service via the Intent.
        serviceIntent.putExtra(LocalService.SEED_KEY, 42L);

        // Bind the service and grab a reference to the binder.
        IBinder binder = serviceRule.bindService(serviceIntent);

        // Get the reference to the service, or you can call
        // public methods on the binder directly.
        LocalService service =
                ((LocalService.LocalBinder) binder).getService();

        // Verify that the service is working correctly.
        assertThat(service.getRandomInt()).isAssignableTo(Integer.class);
    }
    

サービスの統合テストを実行する

統合テストは、Android Studio またはコマンドラインから実行できます。プロジェクトで、デフォルトのインストゥルメンテーション ランナーとして AndroidJUnitRunner を指定してください。

サービスの統合テストを実行するには、テストのスタートガイドで説明されているインストゥルメント化テストの実行手順に従います。

サービスの説明もご覧ください。

参考情報

このトピックの詳細については、次のリソースをご覧ください。

サンプル