How to Mock Android PackageManager for Espresso Test?

Viewed 11

I have the following 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.

0 Answers
Related