在测试中创建意图:“在android.content.Intent中未模拟方法putExtra"; [英] Creating intent in test: "Method putExtra in android.content.Intent not mocked"

查看:287
本文介绍了在测试中创建意图:“在android.content.Intent中未模拟方法putExtra";的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试对广播接收器进行单元测试,该接收器使用JUnit4和Mockito侦听"com.android.music.metachanged"意图.

I'm trying to unit test a broadcast receiver which listens for "com.android.music.metachanged" intents using JUnit4 and Mockito.

广播接收器接收到意图后便开始服务.我想断言该服务已启动.我还想断言,接收到的意图的字符串多余的艺术家"与发送的意图之一相同.

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.

这是我到目前为止所拥有的...

This is what I have so far...

@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<Intent> argument = ArgumentCaptor.forClass(Intent.class);
        verify(mContext, times(1)).startService(argument.capture());

        Intent receivedIntent = argument.getValue();
        assertEquals("SampleArtist", receivedIntent.getStringExtra("artist"));
    }
}

但这会引发java.lang.RuntimeException:android.content.Intent中的方法putExtra不被模拟.

But this fires a java.lang.RuntimeException: Method putExtra in android.content.Intent not mocked.

我检查了,但是我认为OP有一个不同的问题,因为他们没有从测试主体内部发出意图.

I checked this out, but I think the OP had a different problem, since they don't send out an intent from inside the test body.

推荐答案

好的,我看了

All right, I took a look at Method of ContentValues is not mocked as suggested by @Jeff Bowman. Sadly, that question doesn't provide any code, so I hope this will be useful for somebody...

@RunWith(PowerMockRunner.class)
public class MusicBroadcastReceiverUnitTest {
    private MusicBroadcastReceiver mReceiver;

    @Mock
    private Context mContext;

    @Mock
    private Intent androidIntent;

    @Before
    public void setUp() {
        MockitoAnnotations.initMocks(this);

        mReceiver = new MusicBroadcastReceiver();
    }

    @Test
    public void testStartMusicRegistrationService() {
        try {
        PowerMockito.whenNew(Intent.class)
               .withArguments(String.class).thenReturn(androidIntent);
        } catch (Exception e) {
            e.printStackTrace();
        }
        when(androidIntent.getAction())
          .thenReturn("com.android.music.metachanged");
        when(androidIntent.getStringExtra("artist"))
          .thenReturn("SampleArtist");

        mReceiver.onReceive(mContext, intent);

        ArgumentCaptor<Intent> argument = ArgumentCaptor.forClass(Intent.class);
        verify(mContext, times(1)).startService(argument.capture());

        Intent receivedIntent = argument.getValue();
        assertEquals("SampleArtist", receivedIntent.getStringExtra("artist"));
    }
}

是的,我更喜欢嘲笑"getStringExtra"而不是"putExtra".但这对我有用.

So yeah, I rather mocked "getStringExtra" than "putExtra". But it worked for me.

这篇关于在测试中创建意图:“在android.content.Intent中未模拟方法putExtra";的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆