Challenge - 5 Problems
Instrumented Test Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ ui_behavior
intermediate2:00remaining
What is the output of this instrumented test?
Consider this Android instrumented test code snippet. What will the test verify and what is the expected result?
Android Kotlin
import androidx.test.ext.junit.runners.AndroidJUnit4 import androidx.test.rule.ActivityTestRule import org.junit.Rule import org.junit.Test import org.junit.runner.RunWith import androidx.test.espresso.Espresso.onView import androidx.test.espresso.assertion.ViewAssertions.matches import androidx.test.espresso.matcher.ViewMatchers.withText import androidx.test.espresso.matcher.ViewMatchers.withId @RunWith(AndroidJUnit4::class) class MainActivityTest { @get:Rule val activityRule = ActivityTestRule(MainActivity::class.java) @Test fun checkHelloTextDisplayed() { onView(withId(R.id.textView)).check(matches(withText("Hello World"))) } }
Attempts:
2 left
💡 Hint
Look at the onView and check methods to see what UI element and property are tested.
✗ Incorrect
The test uses Espresso to find a view by its id and checks if its text matches "Hello World". If the text matches, the test passes.
❓ lifecycle
intermediate2:00remaining
What happens when an instrumented test uses ActivityScenario.launch()?
In Android instrumented tests, what is the effect of calling ActivityScenario.launch(MainActivity::class.java)?
Attempts:
2 left
💡 Hint
Think about how ActivityScenario helps with lifecycle in tests.
✗ Incorrect
ActivityScenario.launch() starts the activity and handles lifecycle states, allowing tests to interact with the UI safely.
🔧 Debug
advanced2:00remaining
What error will this instrumented test produce?
Given this instrumented test code, what error will occur when running it?
Android Kotlin
import androidx.test.ext.junit.runners.AndroidJUnit4 import androidx.test.rule.ActivityTestRule import org.junit.Rule import org.junit.Test import org.junit.runner.RunWith import androidx.test.espresso.Espresso.onView import androidx.test.espresso.matcher.ViewMatchers.withId import androidx.test.espresso.action.ViewActions.click @RunWith(AndroidJUnit4::class) class ButtonClickTest { @get:Rule val activityRule = ActivityTestRule(MainActivity::class.java) @Test fun clickButton() { onView(withId(R.id.nonExistentButton)).perform(click()) } }
Attempts:
2 left
💡 Hint
Check what happens if Espresso tries to find a view that does not exist.
✗ Incorrect
Espresso throws NoMatchingViewException at runtime if the view with the given id is not found in the current activity.
advanced
2:00remaining
Which option correctly tests navigation between two activities?
You want to write an instrumented test that verifies clicking a button in MainActivity opens DetailActivity. Which code snippet correctly tests this navigation?
Attempts:
2 left
💡 Hint
Think about how to verify the new activity's UI is shown after clicking the button.
✗ Incorrect
After clicking the button, checking that a view unique to DetailActivity is displayed confirms navigation succeeded.
🧠 Conceptual
expert2:00remaining
What is the main difference between local unit tests and instrumented tests in Android?
Choose the best explanation of the key difference between local unit tests and instrumented tests.
Attempts:
2 left
💡 Hint
Consider where each test type runs and what environment they have access to.
✗ Incorrect
Local unit tests run on your computer's JVM and cannot access Android framework classes, while instrumented tests run on real or virtual devices with Android OS.