Я пытаюсь заглушить вызов getExtras () для поддельного намерения, но кажется, что когда () ... thenReturn () не работает (возвращает NULL), когда я использую любую из строковых констант TelephonyManager. Я попытался использовать фиктивные строки, и это работает, поэтому я знаю, что тест не работает из-за вызовов when () ... thenReturn ().
Вот мой код
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());
}
}
Вот тестируемый метод
@Override
public void onReceive(Context context, Intent intent) {
//Getting the phone state
String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);
if(state == null) return;
//Getting the caller's number
callerNumber = intent.getExtras().getString("incoming_number");
if (state.equals(TelephonyManager.EXTRA_STATE_RINGING)) rang = true;
if (state.equals(TelephonyManager.EXTRA_STATE_OFFHOOK)) callReceived = true;
//if the phone is idle
if (state.equals(TelephonyManager.EXTRA_STATE_IDLE)){
if (ringing&&!callReceived){
ringing = false;
try{
Toast.makeText(context, "Missed call", Toast.LENGTH_LONG).show();
}catch (Exception e){
e.printStackTrace();
}
}
callReceived = false;
}
}
Я уже некоторое время искал, не могу найти никаких ответов и не знаю, как еще заглушить намерение этих вызовов getExtras (). Кто-нибудь может мне помочь?