0Pricing
Jetpack Compose Academy · Lesson

createComposeRule & Finders

Set up a Compose test and locate nodes.

createComposeRule & Finders is a free Jetpack Compose Academy lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Jetpack Compose Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

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. 🎉

Frequently asked questions

Is the “createComposeRule & Finders” lesson free?

Yes — the full text of “createComposeRule & Finders” is free to read here on the web, and the Jetpack Compose Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Jetpack Compose Academy course, upgrade to CoddyKit PRO.

What will I learn in “createComposeRule & Finders”?

Set up a Compose test and locate nodes. You practise Jetpack Compose Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Jetpack Compose Academy?

No prior experience is required. Jetpack Compose Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “createComposeRule & Finders” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Jetpack Compose Academy lesson?

Yes. Every Jetpack Compose Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. createComposeRule & Finders
  2. Assertions & Performing Actions
  3. Semantics & Test Tags
  4. Testing ViewModels & Flows
← Back to Jetpack Compose Academy