为什么Mockito的when()... thenReturn()不能与其他类的字符串常量一起使用

时间:2018-08-30 19:30:45

标签: android unit-testing mockito

我正在尝试对模拟意图进行getExtras()调用,但是当我使用任何TelephonyManager字符串常量时,似乎when()... thenReturn()不起作用(返回NULL)。我已经尝试过使用伪字符串并且它可以正常工作,所以我知道由于when()... thenReturn()调用导致测试不起作用。

这是我的代码

Private Sub tbStartDate_AfterUpdate()
    Dim qdef As QueryDef

    Set qdef = CurrentDb.QueryDefs("mySavedUpdateQuery")

    ' BIND VALUE TO PARAMETER
    qdef![DescIDParam] = Me.tbDescriptionID

    ' EXECUTE ACTION
    qdef.Execute dbFailOnError

    Set qdef = Nothing
End Sub

}


这是被测方法

package metabrick.com.notrightnow;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.TelephonyManager;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.mockito.runners.MockitoJUnitRunner;

import metabrick.com.notrightnow.CatchingMissedCallsFeature.MissedCallBroadcastReceiver;

import static junit.framework.Assert.assertFalse;
import static junit.framework.Assert.assertSame;
import static junit.framework.Assert.assertTrue;
import static org.mockito.Matchers.anyString;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

@RunWith(MockitoJUnitRunner.class)


/*This class tests the logic of MissedallBroadcastReceiver class logic .i.e
* The logic of detecting a missed phone call*/

public class MissedCallBroadcastReceiverTest  {
@Mock private Context context;
@Mock private Intent intent;
@Mock Bundle bundle ;

private MissedCallBroadcastReceiver missedCallBroadcastReceiver;

@Before
public void setUpMissedCallBroadcastReceiver(){
    MockitoAnnotations.initMocks(this);
    missedCallBroadcastReceiver = new MissedCallBroadcastReceiver();

}
@Test
public void onReceiveTest(){
    when(intent.getStringExtra(anyString()))
          .thenReturn(telephonyManager.EXTRA_STATE_RINGING);

    when(intent.getExtras()).thenReturn(bundle);

    when(bundle.getString("incoming_number")).thenReturn("905-456-4520");

    //Calling onReceive
    missedCallBroadcastReceiver.onReceive(context, intent);

    //Verfy that attempt was made to retrieve the state of the phone
    verify(intent).getStringExtra(TelephonyManager.EXTRA_STATE);

    //verfy that an attempt was made to retrievr the number of the caller
    verify(bundle).getString("incoming_number");


    /*Testing a received call scenario */
    when(intent.getStringExtra(anyString()))
            .thenReturn(TelephonyManager.EXTRA_STATE_RINGING)
            .thenReturn(TelephonyManager.EXTRA_STATE_OFFHOOK);
    missedCallBroadcastReceiver.onReceive(context, intent);
    //check that the callRinging variable is set to true
    assertTrue(missedCallBroadcastReceiver.isCallRinging());
    //check that the callReceived variable is set to true
    assertTrue(missedCallBroadcastReceiver.isCallReceived());

    /*Testing a missed call scenario */
    when(intent.getStringExtra(anyString()))
            .thenReturn(TelephonyManager.EXTRA_STATE_RINGING)
            .thenReturn(TelephonyManager.EXTRA_STATE_IDLE);
    missedCallBroadcastReceiver.onReceive(context, intent);
    //check that the callReceived and ringing variables are set to false
    assertFalse(missedCallBroadcastReceiver.isCallReceived());
    assertFalse(missedCallBroadcastReceiver.isCallRinging());

    // checking that the number gotten from the intent
    assertSame("905-456-4520", missedCallBroadcastReceiver.getCallerNumber());




}

我已经搜索了一段时间,找不到任何答案,而且我不知道如何将这些调用的意图存根于getExtras()。有人可以帮我吗?

0 个答案:

没有答案