createComposeRule und Finder
Richten Sie einen Compose-Test ein und finden Sie Nodes.
createComposeRule und Finder ist eine kostenlose Jetpack Compose Academy-Lektion auf CoddyKit. Dies ist Lektion 1 von 4. Du kannst die komplette Lektion unten kostenlos lesen – dann übst du sie direkt im Browser mit einem integrierten Code-Editor und einem KI-Tutor rund um die Uhr. Sie ist Teil des Jetpack Compose Academy-Lernpfads, und dein Fortschritt wird über Web und CoddyKit-App synchronisiert. Der Jetpack Compose Academy-Kurs umfasst insgesamt 4 Lektionen.
Teile dieser Lektion wurden noch nicht übersetzt und werden auf Englisch angezeigt.
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. 🎉
Häufig gestellte Fragen
Ist die Lektion „createComposeRule und Finder“ kostenlos?
Ja — der vollständige Text von „createComposeRule und Finder“ ist hier im Web kostenlos zu lesen. Um sie interaktiv zu üben (integrierter Code-Editor und 24/7 KI-Tutor) und den Rest des Jetpack Compose Academy-Kurses freizuschalten, upgrade auf CoddyKit PRO. Der Jetpack Compose Academy-Kurs umfasst insgesamt 4 Lektionen.
Was lerne ich in „createComposeRule und Finder“?
Richten Sie einen Compose-Test ein und finden Sie Nodes. Du übst Jetpack Compose Academy mit praktischem Code, den du direkt im Browser ausführst, und ein 24/7 KI-Tutor beantwortet deine Fragen während du die Lektion bearbeitest.
Brauche ich Erfahrung, um Jetpack Compose Academy zu starten?
Keine Vorkenntnisse erforderlich. Jetpack Compose Academy auf CoddyKit ist für Anfänger bis fortgeschrittene Lernende strukturiert, sodass du hier starten oder von Anfang an beginnen und in deinem eigenen Tempo voranschreiten kannst. Dies ist Lektion 1 von 4.
Wie lange dauert die Lektion „createComposeRule und Finder“?
Die meisten CoddyKit-Lektionen dauern etwa 5–10 Minuten. Jede ist kompakt und interaktiv, sodass du stetig Fortschritte machst und genau dort weitermachst, wo du aufgehört hast – im Web und in der App.
Kann ich in dieser Jetpack Compose Academy-Lektion Code schreiben und ausführen?
Ja. Jede Jetpack Compose Academy-Lektion enthält einen integrierten Code-Editor, sodass du echten Code direkt in deinem Browser schreibst und ausführst und sofort KI-Feedback erhältst — ohne lokale Einrichtung erforderlich.
Alle Lektionen in diesem Kurs
- createComposeRule und Finder
- Assertions und Aktionen ausführen
- Semantik und Test-Tags
- ViewModels und Flows testen