Skip to content

Commit b3cb518

Browse files
committed
Adds Espresso test for checking for a toast
1 parent f595c43 commit b3cb518

File tree

3 files changed

+48
-29
lines changed

3 files changed

+48
-29
lines changed

com.vogella.android.daggerjunitmockito/app/src/androidTest/java/com/vogella/android/daggerjunitmockito/ExampleInstrumentationTest.java

-29
This file was deleted.

com.vogella.android.daggerjunitmockito/app/src/main/java/com/vogella/android/daggerjunitmockito/MainActivity.java

+8
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,20 @@
22

33
import android.app.Activity;
44
import android.os.Bundle;
5+
import android.widget.Toast;
6+
7+
import javax.inject.Inject;
58

69
public class MainActivity extends Activity {
710

11+
@Inject
12+
MainService service;
13+
814
@Override
915
protected void onCreate(Bundle savedInstanceState) {
1016
super.onCreate(savedInstanceState);
1117
setContentView(R.layout.activity_main);
18+
19+
Toast.makeText(this, service.doSomething(), Toast.LENGTH_LONG).show();
1220
}
1321
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package com.vogella.android.test.juntexamples;
2+
3+
import android.support.test.rule.ActivityTestRule;
4+
import android.support.test.runner.AndroidJUnit4;
5+
6+
import org.junit.Rule;
7+
import org.junit.Test;
8+
import org.junit.runner.RunWith;
9+
10+
import static android.support.test.espresso.Espresso.onData;
11+
import static android.support.test.espresso.Espresso.onView;
12+
import static android.support.test.espresso.action.ViewActions.click;
13+
import static android.support.test.espresso.assertion.ViewAssertions.matches;
14+
import static android.support.test.espresso.matcher.RootMatchers.withDecorView;
15+
import static android.support.test.espresso.matcher.ViewMatchers.isDisplayed;
16+
import static android.support.test.espresso.matcher.ViewMatchers.withText;
17+
import static org.hamcrest.CoreMatchers.containsString;
18+
import static org.hamcrest.CoreMatchers.is;
19+
import static org.hamcrest.CoreMatchers.startsWith;
20+
import static org.hamcrest.Matchers.greaterThan;
21+
import static org.hamcrest.Matchers.hasToString;
22+
import static org.hamcrest.Matchers.instanceOf;
23+
import static org.hamcrest.Matchers.not;
24+
import static org.hamcrest.Matchers.notNullValue;
25+
import static org.junit.Assert.assertThat;
26+
27+
@RunWith(AndroidJUnit4.class)
28+
public class MainActivityTestList {
29+
30+
@Rule
31+
public ActivityTestRule<MainActivity> rule = new ActivityTestRule<>(MainActivity.class);
32+
33+
@Test
34+
public void ensureToastIsDisplayedIfListItemIsClicked() throws Exception {
35+
onData(hasToString(containsString("Frodo"))).perform(click());
36+
onView(withText(startsWith("Clicked:")))
37+
.inRoot(withDecorView(not(is(rule.getActivity().getWindow().getDecorView()))))
38+
.check(matches(isDisplayed()));
39+
}
40+
}

0 commit comments

Comments
 (0)