0
0
Android Kotlinmobile~10 mins

Instrumented tests in Android Kotlin - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to annotate an instrumented test class.

Android Kotlin
import androidx.test.ext.junit.runners.AndroidJUnit4

@[1]
class ExampleInstrumentedTest {
}
Drag options to blanks, or click blank then click option'
ABefore
BRunWith(AndroidJUnit4::class)
CTest
DIgnore
Attempts:
3 left
💡 Hint
Common Mistakes
Using @Test instead of @RunWith for the class annotation.
Forgetting to specify the test runner causes the test not to run on device.
2fill in blank
medium

Complete the code to mark a function as an instrumented test.

Android Kotlin
import org.junit.Test

class ExampleInstrumentedTest {
    @[1]
    fun useAppContext() {
        // test code here
    }
}
Drag options to blanks, or click blank then click option'
ATest
BBefore
CIgnore
DAfter
Attempts:
3 left
💡 Hint
Common Mistakes
Using @Before or @After instead of @Test for test methods.
Forgetting to annotate test functions causes them not to run.
3fill in blank
hard

Fix the error in the code to get the app context in an instrumented test.

Android Kotlin
import androidx.test.platform.app.InstrumentationRegistry

val appContext = InstrumentationRegistry.[1].targetContext
Drag options to blanks, or click blank then click option'
AgetInstrumentation()
BgetContext()
CgetTargetContext()
DgetApplicationContext()
Attempts:
3 left
💡 Hint
Common Mistakes
Calling getContext() or getTargetContext() directly on InstrumentationRegistry causes errors.
Using getApplicationContext() is not available here.
4fill in blank
hard

Fill both blanks to launch an activity in an instrumented test.

Android Kotlin
import androidx.test.core.app.ActivityScenario

val scenario = ActivityScenario.[1](MainActivity::class.[2])
Drag options to blanks, or click blank then click option'
Alaunch
Bjava
Cstart
Dkotlin
Attempts:
3 left
💡 Hint
Common Mistakes
Using start() instead of launch() causes errors.
Passing MainActivity::class without .java causes type errors.
5fill in blank
hard

Fill all three blanks to assert the app package name in an instrumented test.

Android Kotlin
import androidx.test.platform.app.InstrumentationRegistry
import org.junit.Assert.*

val appContext = InstrumentationRegistry.[1].targetContext
assertEquals("[2]", appContext.[3].packageName)
Drag options to blanks, or click blank then click option'
AgetInstrumentation()
Bcom.example.myapp
CapplicationContext
DgetContext()
Attempts:
3 left
💡 Hint
Common Mistakes
Using getContext() instead of getInstrumentation() causes errors.
Accessing packageName directly on appContext without applicationContext may cause issues.