1+ package com .vogella .android .test .juntexamples .mockito ;
2+
3+ import android .content .Context ;
4+ import android .content .Intent ;
5+ import android .support .test .runner .AndroidJUnit4 ;
6+
7+ import com .vogella .android .test .juntexamples .OutgoingCallReceiver ;
8+
9+ import org .junit .Before ;
10+ import org .junit .Test ;
11+ import org .junit .runner .RunWith ;
12+ import org .mockito .ArgumentCaptor ;
13+
14+ import static junit .framework .Assert .assertEquals ;
15+ import static junit .framework .Assert .assertTrue ;
16+ import static org .junit .Assert .assertNull ;
17+ import static org .mockito .Mockito .mock ;
18+ import static org .mockito .Mockito .times ;
19+ import static org .mockito .Mockito .verify ;
20+
21+ @ RunWith (AndroidJUnit4 .class )
22+ public class OutgoingCallReceiverTest {
23+ private OutgoingCallReceiver mReceiver ;
24+ private Context mContext ;
25+
26+ @ Before
27+ public void setUp () {
28+ mReceiver = new OutgoingCallReceiver ();
29+ mContext = mock (Context .class );
30+ }
31+
32+
33+
34+ @ Test
35+ public void activityShouldGetCorrectIntentData () {
36+ // prepare data for onReceive and call it
37+ Intent intent = new Intent (Intent .ACTION_NEW_OUTGOING_CALL );
38+ intent .putExtra (Intent .EXTRA_PHONE_NUMBER , "01234567890" );
39+
40+ mReceiver .onReceive (mContext , intent );
41+ assertNull (mReceiver .getResultData ());
42+
43+ // what did receiver do?
44+ ArgumentCaptor <Intent > argument =
45+ ArgumentCaptor .forClass (Intent .class );
46+ verify (mContext , times (1 )).startActivity (argument .capture ());
47+ Intent receivedIntent = argument .getValue ();
48+ assertNull (receivedIntent .getAction ());
49+ assertEquals ("01234567890" , receivedIntent .getStringExtra ("phoneNum" ));
50+ assertTrue ((receivedIntent .getFlags () &
51+ Intent .FLAG_ACTIVITY_NEW_TASK ) != 0 );
52+ }
53+ }
0 commit comments