Android application development 第26章:UIテスト(Espresso)

☕ Espressoとは?

Espresso(エスプレッソ) は、Google が提供する Android の UI テストフレームワーク です。

Java の単体テスト(JUnit)では画面の操作はできませんが、Espresso を使えば実際のユーザー操作を自動化してテストできます。

たとえば…

  • ボタンを押す
  • テキストを入力する
  • 別の画面に遷移する
  • 表示されているテキストを確認する

といった処理を 自動で実行して検証できます。


🔍 なぜ必要なのか?

画面をタップして確認する手動テストは…

  • 時間がかかる
  • ミスが起きやすい
  • リファクタで壊れても気づけない

という課題があります。

Espresso による UI テストを使えば、アプリの画面遷移や表示状態をコードで保証できるため、信頼性の高い開発が可能になります。


⚙ セットアップ

build.gradle に以下を追加します:

android {
    defaultConfig {
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }
}

dependencies {
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'
    androidTestImplementation 'androidx.test.ext:junit:1.1.5'
}

UIテスト用のコードは src/androidTest/java/ に書きます(test/ ではなく)。


👀 基本構文

Espresso は以下のような構文で操作します:

onView(withId(R.id.button))
    .perform(click());

onView(withId(R.id.textView))
    .check(matches(withText("Hello")));

読みやすいように整理すると:

処理メソッド例説明
UI要素の取得onView(withId(...))ボタン、テキストなどの要素を指定
操作.perform(...)クリック、入力などの操作
検証.check(...)表示内容や状態を確認

🧪 サンプル:ボタンを押してテキストが変わる

テスト対象のActivityのコード

public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button btn = findViewById(R.id.button);
        TextView txt = findViewById(R.id.textView);

        btn.setOnClickListener(v -> txt.setText("Hello Espresso!"));
    }
}

Espressoテストコード

@RunWith(AndroidJUnit4.class)
public class MainActivityTest {

    @Rule
    public ActivityScenarioRule<MainActivity> rule =
        new ActivityScenarioRule<>(MainActivity.class);

    @Test
    public void testButtonClick_changesTextView() {
        // ボタンをクリック
        onView(withId(R.id.button)).perform(click());

        // テキストが変更されたかを確認
        onView(withId(R.id.textView))
            .check(matches(withText("Hello Espresso!")));
    }
}

💡 よく使う操作例

操作コード例
ボタンを押すperform(click())
テキスト入力perform(typeText("Hello"))
ソフトキーボードを閉じるperform(closeSoftKeyboard())
表示されていることを確認check(matches(isDisplayed()))

⚠ Javaエンジニア向けの注意点

  • JUnitとは別のランナー(InstrumentationRunner)で動きます
    • UI を実際に操作するため、エミュレータや実機が必要です。
  • Android Studio の androidTest タブから実行します。
  • テストは Activity 起動後に実行されるため、UIの準備が整うまで待機処理が自動で入るのが Espresso の強み(Wait不要)。

✅ まとめ

項目内容
対象画面操作、UI表示確認など
ツールEspresso(Google製)
記述場所src/androidTest/java/
実行方法Android Studio or CLI からエミュレータ/実機で実行
特徴実ユーザー操作に近いテストができる。UIの状態に賢く同期してくれる。

💬 補足

  • 画面遷移のテスト(Intentの発行確認)には Intents ライブラリも使えます。
  • MVVM構成では ViewModel をユニットテストし、UIレイヤーだけ Espresso でテストする分担が理想です。

mh

Related Posts

Android application development 第30章:Firebase Crashlytics でのクラッシュレポート

Android application development 第29章:キーストアと署名

You Missed

現場で使えるChrome DevTools実践ガイド 第6章:Sourcesパネルでのブレークポイントとステップ実行

  • 投稿者 mh
  • 5月 30, 2025
  • 6 views

Kubernetes Learning 第26章:標準的なログ取得方法(kubectl logs)〜Podの中で何が起きているかをのぞいてみよう〜

  • 投稿者 mh
  • 5月 30, 2025
  • 0 views

Android application development 第30章:Firebase Crashlytics でのクラッシュレポート

  • 投稿者 mh
  • 5月 30, 2025
  • 0 views

Kubernetes Learning 第25章:Replica数と冗長性の設計 〜止まらないシステムのために〜

  • 投稿者 mh
  • 5月 29, 2025
  • 12 views

Android application development 第29章:キーストアと署名

  • 投稿者 mh
  • 5月 29, 2025
  • 13 views

現場で使えるChrome DevTools実践ガイド 第5章:Consoleパネルの活用

  • 投稿者 mh
  • 5月 28, 2025
  • 16 views