Bird
Raised Fist0
Android Kotlinmobile~20 mins

Instrumented tests in Android Kotlin - Practice Problems & Coding Challenges

Choose your learning style10 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.

Practice

(1/5)
1. What is the main purpose of instrumented tests in Android development?
easy
A. To test only the backend logic without UI
B. To write unit tests that run on the local JVM
C. To run tests on real devices or emulators to check app behavior
D. To create UI layouts for the app

Solution

  1. Step 1: Understand what instrumented tests do

    Instrumented tests run on actual devices or emulators to test app behavior in a real environment.
  2. Step 2: Compare with other test types

    Unit tests run locally on JVM and do not test device features or UI on real devices.
  3. Final Answer:

    To run tests on real devices or emulators to check app behavior -> Option C
  4. Quick Check:

    Instrumented tests = run on device/emulator [OK]
Hint: Instrumented tests run on devices, not just local JVM [OK]
Common Mistakes:
  • Confusing instrumented tests with unit tests
  • Thinking instrumented tests only test backend logic
  • Assuming instrumented tests create UI layouts
2. Which annotation is required to run an instrumented test with AndroidJUnit4?
easy
A. @RunWith(AndroidJUnit4::class)
B. @Ignore
C. @Before
D. @Test

Solution

  1. Step 1: Identify the runner annotation

    Instrumented tests need the @RunWith(AndroidJUnit4::class) annotation to specify the test runner.
  2. Step 2: Differentiate from other annotations

    @Test marks test methods, @Before runs setup code, @Ignore skips tests; none specify the runner.
  3. Final Answer:

    @RunWith(AndroidJUnit4::class) -> Option A
  4. Quick Check:

    Runner annotation = @RunWith(AndroidJUnit4::class) [OK]
Hint: Use @RunWith(AndroidJUnit4::class) to set test runner [OK]
Common Mistakes:
  • Using @Test instead of @RunWith for runner
  • Confusing @Before with runner annotation
  • Forgetting to add any runner annotation
3. Given this instrumented test code snippet, what will be the output?
val appContext = InstrumentationRegistry.getInstrumentation().targetContext
val packageName = appContext.packageName
println(packageName)
medium
A. An empty string
B. The package name of the app under test
C. The package name of the test app
D. A runtime exception

Solution

  1. Step 1: Understand InstrumentationRegistry usage

    InstrumentationRegistry.getInstrumentation().targetContext returns the context of the app under test, not the test app.
  2. Step 2: Extract package name from app context

    appContext.packageName gives the package name of the app being tested, which is printed.
  3. Final Answer:

    The package name of the app under test -> Option B
  4. Quick Check:

    targetContext.packageName = app package [OK]
Hint: targetContext is app under test, not test app [OK]
Common Mistakes:
  • Confusing targetContext with testContext
  • Expecting empty string instead of package name
  • Assuming runtime exception without cause
4. What is wrong with this instrumented test code?
@RunWith(AndroidJUnit4::class)
class ExampleInstrumentedTest {
  @Test
  fun useAppContext() {
    val context = InstrumentationRegistry.getContext()
    assertEquals("com.example.app", context.packageName)
  }
}
medium
A. InstrumentationRegistry.getContext() is deprecated; should use getInstrumentation().targetContext
B. Missing @Before annotation for setup
C. assertEquals syntax is incorrect
D. Missing @RunWith annotation

Solution

  1. Step 1: Check InstrumentationRegistry usage

    InstrumentationRegistry.getContext() is deprecated and does not return the app context; getInstrumentation().targetContext should be used.
  2. Step 2: Verify other parts of the code

    @RunWith is present, assertEquals syntax is correct, and @Before is not required here.
  3. Final Answer:

    InstrumentationRegistry.getContext() is deprecated; should use getInstrumentation().targetContext -> Option A
  4. Quick Check:

    Use getInstrumentation().targetContext, not getContext() [OK]
Hint: Use getInstrumentation().targetContext, not getContext() [OK]
Common Mistakes:
  • Using deprecated getContext() method
  • Assuming @Before is mandatory for all tests
  • Thinking assertEquals syntax is wrong
5. You want to write an instrumented test that verifies a button click updates a TextView on a real device. Which steps should you follow to correctly implement this test?
hard
A. Use @RunWith(AndroidJUnit4::class), get test context, launch activity, perform click, check TextView text
B. Write a unit test with @Test, mock the button click, and check TextView text without device
C. Write a UI test without annotations, directly call button click method and check TextView
D. Use @RunWith(AndroidJUnit4::class), get app context with InstrumentationRegistry.getInstrumentation().targetContext, launch activity, perform click, check TextView text

Solution

  1. Step 1: Setup instrumented test with correct runner and context

    Use @RunWith(AndroidJUnit4::class) and get app context via InstrumentationRegistry.getInstrumentation().targetContext to access real app environment.
  2. Step 2: Launch activity, perform UI action, and verify result

    Launch the activity, simulate button click, and check if TextView text updates as expected on device/emulator.
  3. Final Answer:

    Use @RunWith(AndroidJUnit4::class), get app context with InstrumentationRegistry.getInstrumentation().targetContext, launch activity, perform click, check TextView text -> Option D
  4. Quick Check:

    Instrumented UI test = runner + app context + UI actions [OK]
Hint: Instrumented UI tests need real context and runner [OK]
Common Mistakes:
  • Using test context instead of app context
  • Writing unit tests for UI behavior
  • Skipping @RunWith annotation
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard