title: How can I shadow the PackageManager with Robolectric
body: <p>My Android application has a simple method to fire off an intent to display a URL.  </p>
  
  <pre><code>protected void launchBrowser(int id)
  {
      Uri uri = Uri.parse( getString( id ) );
      Intent intent = new Intent( ACTION_VIEW, uri);
  
      PackageManager packageManager = getPackageManager();
      List&lt;ResolveInfo&gt; activities = packageManager.queryIntentActivities(intent, 0);
      if (activities.size() &gt; 0)
      {
          startActivity(intent);
      }
      else
      {
          Toast.makeText(getApplicationContext(),
                         "ERROR - no application to display a web page",
                         Toast.LENGTH_SHORT).show();
      }
  }
  </code></pre>
  
  <p>I'm using Robolectric for unit testing but I'm having trouble verifying this method.  Specifically, <code>getPackageManager()</code> is always returning null.  How I can shadow the <code>PackageManager</code>?  I tried creating a <code>ShadowPackageManager</code> and calling <code>bindShadowClass</code>, but none of my code gets executed - <code>getPackageManager()</code> always returns <code>null</code>.  I also tried to Shadow the Application context and return a concrete <code>StubPackageManager</code>, but got the same results.
  Maybe I've been searching/staring too long - is there a better way to unit test this method?</p>