title: Creating intent in test: "Method putExtra in android.content.Intent not mocked"
body: <p>I'm trying to unit test a broadcast receiver which listens for "com.android.music.metachanged" intents using JUnit4 and Mockito.</p>
  
  <p>The broadcast receiver starts a service when it receives an intent. I want to assert that the service is started. I also want to assert that the string extra "artist" of the received intent is the same as the one of the sent intent.</p>
  
  <p>This is what I have so far...</p>
  
  <pre><code>@RunWith(PowerMockRunner.class)
  public class MusicBroadcastReceiverUnitTest {
      private MusicBroadcastReceiver mReceiver;
  
      @Mock
      private Context mContext;
  
      @Before
      public void setUp() {
          MockitoAnnotations.initMocks(this);
  
          mReceiver = new MusicBroadcastReceiver();
      }
  
      @Test
      public void testStartMusicRegistrationService() {
          Intent intent = new Intent("com.android.music.metachanged");
          intent.putExtra("artist", "SampleArtist");
  
          mReceiver.onReceive(mContext, intent);
          assertNull(mReceiver.getResultData());
  
          ArgumentCaptor&lt;Intent&gt; argument = ArgumentCaptor.forClass(Intent.class);
          verify(mContext, times(1)).startService(argument.capture());
  
          Intent receivedIntent = argument.getValue();
          assertEquals("SampleArtist", receivedIntent.getStringExtra("artist"));
      }
  }
  </code></pre>
  
  <p>But this fires a java.lang.RuntimeException: Method putExtra in android.content.Intent not mocked.</p>
  
  <p>I checked <a href="https://stackoverflow.com/questions/38617398/runtimeexception-method-putextra-in-android-content-intent-not-mocked">this</a> out, but I think the OP had a different problem, since they don't send out an intent from inside the test body.</p>