0

我是 Android 新手,正在尝试测试广播接收器。下面的代码中的 Intent 操作为 null,并且通过 NullPointerException。

    @Test
    public void TestAttachTimeoutIntent() {

        String intentAction = Constants.actionPrefix + Constants.actionAttach;
        Context mockContext = mock(Context.class);
        SgsLiteBroadcastReceiver broadcastReceiver = new SgsLiteBroadcastReceiver(null, null);

        Intent attachIntent = new Intent(intentAction);
        attachIntent.putExtra("result", RESULT_SUCCESS);
        attachIntent.putExtra("sequence_number", 3);

        broadcastReceiver.onReceive(mockContext, attachIntent);
        assertNull(broadcastReceiver.getResultData());



        ArgumentCaptor<Intent> argumentIntent = ArgumentCaptor.forClass(Intent.class);
        Mockito.verify(mockContext, Mockito.times(1)).startActivity(argumentIntent.capture());


        Intent receivedIntent = argumentIntent.getValue();
        assertNull(receivedIntent.getAction());
        assertEquals("result", receivedIntent.getStringExtra("result"));
        assertTrue((receivedIntent.getIntExtra("sequence_number", 0) != 0));
    }
4

0 回答 0