How can I shadow the PackageManager with Robolectric

后端 未结 6 835
有刺的猬
有刺的猬 2020-12-15 22:16

My Android application has a simple method to fire off an intent to display a URL.

protected void launchBrowser(int id)
{
    Uri uri = Uri.parse( getStrin         


        
6条回答
  •  悲哀的现实
    2020-12-15 23:02

    I'm using Robolectric 2.3 for this. As noted in other answers, getPackageManager() does not return null, but shadowApplication.setPackageManager no longer exists.

    Since you can't mock PackageManager, you can't give it a list of Intents to resolve for. Fortunately, Robolectric's PackageManager subclass, RobolectricPackageManager, can let you add these intents without a mock:

    RobolectricPackageManager rpm = (RobolectricPackageManager)Robolectric.application.getPackageManager();
    rpm.addResolveInfoForIntent(new Intent(Settings.ACTION_APPLICATION_DEVELOPMENT_SETTINGS), new ResolveInfo());
    

提交回复
热议问题