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
Step 1: Understand InstrumentationRegistry usage
InstrumentationRegistry.getInstrumentation().targetContext returns the context of the app under test, not the test app.
Step 2: Extract package name from app context
appContext.packageName gives the package name of the app being tested, which is printed.
Final Answer:
The package name of the app under test -> Option B
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
Step 1: Check InstrumentationRegistry usage
InstrumentationRegistry.getContext() is deprecated and does not return the app context; getInstrumentation().targetContext should be used.
Step 2: Verify other parts of the code
@RunWith is present, assertEquals syntax is correct, and @Before is not required here.
Final Answer:
InstrumentationRegistry.getContext() is deprecated; should use getInstrumentation().targetContext -> Option A
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
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.
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.
Final Answer:
Use @RunWith(AndroidJUnit4::class), get app context with InstrumentationRegistry.getInstrumentation().targetContext, launch activity, perform click, check TextView text -> Option D