0
0
Android Kotlinmobile~20 mins

Instrumented tests in Android Kotlin - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Instrumented Test Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
ui_behavior
intermediate
2: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")))
  }
}
AThe test will fail because ActivityTestRule is deprecated and cannot be used.
BThe test checks if the TextView with id textView is clickable and passes if true.
CThe test verifies if the MainActivity launches without crashing but does not check UI text.
DThe test checks if the TextView with id textView displays "Hello World" and passes if true.
Attempts:
2 left
💡 Hint
Look at the onView and check methods to see what UI element and property are tested.
lifecycle
intermediate
2: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)?
AIt launches MainActivity but does not allow interaction with UI elements.
BIt only creates an instance of MainActivity but does not start it.
CIt launches the MainActivity and manages its lifecycle automatically for testing.
DIt launches MainActivity but immediately finishes it, so tests cannot run.
Attempts:
2 left
💡 Hint
Think about how ActivityScenario helps with lifecycle in tests.
🔧 Debug
advanced
2: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())
  }
}
ANo error, test passes because the button exists.
BRuntimeException: NoMatchingViewException because the button with id nonExistentButton is not found.
CCompilation error because R.id.nonExistentButton is undefined.
DTest fails with NullPointerException due to missing activityRule.
Attempts:
2 left
💡 Hint
Check what happens if Espresso tries to find a view that does not exist.
navigation
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?
A
onView(withId(R.id.openDetailButton)).perform(click())
onView(withId(R.id.detailRootLayout)).check(matches(isDisplayed()))
B
onView(withId(R.id.openDetailButton)).perform(click())
onView(withId(R.id.mainRootLayout)).check(matches(isDisplayed()))
C
onView(withId(R.id.openDetailButton)).perform(click())
assertEquals(currentActivity, MainActivity::class.java)
D
onView(withId(R.id.openDetailButton)).perform(click())
assertEquals(currentActivity, DetailActivity::class.java)
onView(withId(R.id.detailRootLayout)).check(matches(isDisplayed()))
Attempts:
2 left
💡 Hint
Think about how to verify the new activity's UI is shown after clicking the button.
🧠 Conceptual
expert
2: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.
ALocal unit tests run on the JVM without Android framework, instrumented tests run on a device or emulator with full Android environment.
BLocal unit tests can test UI components, instrumented tests cannot test UI.
CLocal unit tests require a device or emulator, instrumented tests run on the JVM only.
DLocal unit tests are slower than instrumented tests because they run on devices.
Attempts:
2 left
💡 Hint
Consider where each test type runs and what environment they have access to.