0Pricing
Jetpack Compose Academy · レッスン

createComposeRuleとFinder

Composeのテストを設定し、ノードを見つけます。

「createComposeRuleとFinder」はCoddyKit上の無料Jetpack Compose Academyレッスンです。 これはレッスン1/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはJetpack Compose Academy学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 Jetpack Compose Academyコースには全4レッスンが含まれています。

このレッスンの一部はまだ翻訳されておらず、英語で表示されています。

Why Test Your UI?

Manual testing is slow and easy to forget. A UI test launches your Composable and checks it automatically, catching breakages before your users ever do. 🧪

Meet the Compose Test Rule

Every Compose test starts with a test rule. It hosts your Composables in a controlled environment so you can render, inspect, and interact with them from test code.

createComposeRule

For pure UI with no Activity, createComposeRule gives you a lightweight rule. You attach it as a JUnit @get:Rule field in your test class.

@get:Rule
val composeTestRule = createComposeRule()

setContent in a Test

Use setContent on the rule to render the Composable you want to verify. This is your test's UI, just like in a real Activity.

composeTestRule.setContent {
    Greeting(name = "Ada")
}

Finders Locate Nodes

Once your UI is on screen, you need a finder to grab a specific element. Finders search the semantics tree and return a node you can assert on.

onNodeWithText

The friendliest finder is onNodeWithText. Give it the text shown on screen and it returns that exact node for you to check.

composeTestRule.onNodeWithText("Hello, Ada!")

onNodeWithContentDescription

For images and icons with no visible text, use onNodeWithContentDescription. It matches the accessibility label, which keeps tests and screen readers in sync.

composeTestRule.onNodeWithContentDescription("Profile photo")

One Node vs Many

onNode finds a single match and fails if there are several. When you expect a group of elements, reach for onAllNodes to get a collection instead.

composeTestRule.onAllNodesWithText("Item")

Quick Check

You want to locate an icon button that has no visible text. Which finder fits best?

Finders Are Lazy

A finder does not search immediately. It captures your intent, and the actual lookup happens when you call an assertion or action on it.

Helpful Failure Messages

When a finder matches nothing, the test fails with a clear message and even prints the current node tree. That makes debugging a missing element fast.

createAndroidComposeRule

Need a real Activity, resources, or navigation? Swap in createAndroidComposeRule, which launches an Activity and still exposes the same finder API.

@get:Rule
val rule = createAndroidComposeRule<MainActivity>()

Recap: Rules & Finders

You set up a compose rule, render with setContent, then locate elements with finders like onNodeWithText. That foundation powers every Compose test you write. 🎉

よくある質問

「createComposeRuleとFinder」レッスンは無料ですか?

はい。「createComposeRuleとFinder」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Jetpack Compose Academyコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Jetpack Compose Academyコースには全4レッスンが含まれています。

「createComposeRuleとFinder」で何を学びますか?

Composeのテストを設定し、ノードを見つけます。 ブラウザで直接実行するハンズオンコードでJetpack Compose Academyを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。

Jetpack Compose Academyを始めるのに経験は必要ですか?

事前経験は必要ありません。CoddyKitのJetpack Compose Academyは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン1/4です。

「createComposeRuleとFinder」レッスンにはどのくらい時間がかかりますか?

ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。

このJetpack Compose Academyレッスンでコードを書いて実行できますか?

はい。すべてのJetpack Compose Academyレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。

このコースのすべてのレッスン

  1. createComposeRuleとFinder
  2. アサーションとアクションの実行
  3. セマンティクスとテストタグ
  4. ViewModelとFlowのテスト
← Jetpack Compose Academyに戻る