Skip to content

Commit ef086f3

Browse files
committed
Adds broadcast receiver test and updates Mockito
1 parent d85c8ed commit ef086f3

File tree

3 files changed

+79
-5
lines changed

3 files changed

+79
-5
lines changed

com.vogella.android.test.juntexamples/app/build.gradle

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,11 @@ dependencies {
3131
compile fileTree(dir: 'libs', include: ['*.jar'])
3232
compile 'com.android.support.constraint:constraint-layout:1.0.0-alpha3'
3333
testCompile 'junit:junit:4.12'
34-
testCompile "org.mockito:mockito-all:1.10.19"
34+
testCompile 'org.mockito:mockito-core:2.7.22'
35+
// required if you want to use Mockito for Android tests
36+
androidTestCompile 'org.mockito:mockito-android:2.7.22'
3537
testCompile 'org.hamcrest:hamcrest-library:1.3'
3638
androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.2'
3739
androidTestCompile 'com.android.support.test:runner:0.5'
3840
androidTestCompile 'com.android.support:support-annotations:24.0.0'
39-
testCompile "org.mockito:mockito-all:1.10.19"
40-
androidTestCompile 'org.mockito:mockito-core:2.0.57-beta'
41-
androidTestCompile "com.google.dexmaker:dexmaker:1.2"
42-
androidTestCompile "com.google.dexmaker:dexmaker-mockito:1.2"
4341
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
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+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.vogella.android.test.juntexamples;
2+
3+
import android.content.BroadcastReceiver;
4+
import android.content.Context;
5+
import android.content.Intent;
6+
7+
/**
8+
* Created by vogella on 11.04.17.
9+
*/
10+
11+
public class OutgoingCallReceiver extends BroadcastReceiver {
12+
@Override
13+
public void onReceive(Context ctx, Intent i) {
14+
if(i.getAction().equalsIgnoreCase(Intent.ACTION_NEW_OUTGOING_CALL))
15+
{
16+
String phoneNum = i.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
17+
Intent intent = new Intent(ctx, MainActivity.class);
18+
intent.putExtra("phoneNum", phoneNum);
19+
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
20+
ctx.startActivity(intent);
21+
}
22+
}
23+
}

0 commit comments

Comments
 (0)