Just wondering if anyone knows how to test this sc...
# android
a
Just wondering if anyone knows how to test this scenario?
Copy code
fun someFunctionInsideActivity() {
    val intent = try {
        packageManager.getLaunchIntentForPackage("some.app.id")
    } catch (e: Exception) {
        Intent(
            Intent.ACTION_VIEW,
            Uri.parse(webSiteUrl)
        )
    }
    startActivity(intent)
}
I want to write
Espresso
test for both scenario. I was wondering if its possible to Mock the return of
packageManager.getLaunchIntentForPackage(...)
so that I could test both scenario in test. Any ideas? Any other way of testing this would be appreciated. Thanks in advance.
😶 2
j
Have a facade around the real packageManager which you can then mock in testing.
Here https://github.com/lambdasoup/watchlater/blob/master/app/src/androidTest/java/com/lambdasoup/watchlater/AppModuleExtra.kt (and the project around it) is an example for how to do that. This one uses koin, but of course any DI framework enables you to do the same.
a
@Juliane Lehmann Thanks for sharing will try this out.
e
Robolectric has a ShadowPackageManager that can be used to alter PackageManager behavior. not as an instrumented test of course
a
@ephemient can you share more details?