0
\$\begingroup\$

I have recently began writing tests for Android using the espresso library. Here I have three tests.

The first test is to check if a snackbar appears when searching with blank text. The second is a search for a restaraunt not in the database. Finally I have a test that checks if there is an internet connection. Can anyone verify that my tests are doing what I intend them to do and if there is anything I am missing or doing wrong?

    public class MainScreenTest {


    //Rules are interceptors which are executed for each test method
    //and will run before any set up code in the @Before method
    @Rule
   public ActivityTestRule<MainScreen> mainScreenActivityTestRule =
            new ActivityTestRule<MainScreen>(MainScreen.class);


    @Test
    public void testSearchWithBlankText() throws Exception{
        Espresso.onView(withId(R.id.searchEditText)).perform(typeText(""),pressImeActionButton());
        Espresso.onView(withText(R.string.invalidSearch))
                .check(matches(isDisplayed()));

    }

    @Test
    public void testSearchWithInvalidRestaurant() throws Exception{
        Espresso.onView(withId(R.id.searchEditText))
                .perform(typeText("Hoffman"),pressImeActionButton());
        Espresso.onView(withId(R.id.noResultsTV)).check(matches(withText("No results, try another restaurant")));
    }


    @Test
    public void testWhenInternetIsAvailable() throws Exception{
        Context context = mainScreenActivityTestRule.getActivity().getBaseContext();
        ConnectivityManager cm = (ConnectivityManager)
                context.getSystemService(Context.CONNECTIVITY_SERVICE);
        boolean isConnected = cm.getActiveNetworkInfo().isConnectedOrConnecting();
        Assert.assertEquals(true, isConnected);
    }




}
\$\endgroup\$
1
\$\begingroup\$
  1. Test#1 looks good, but I would add a case with blank text "_____" too.
  2. Test#2 looks good, but I would name as userGetsErrorWhenSearchingInvalidRestaurant()
  3. Test#3 needs attention. You verify only that an internet connection is good but you don't test user interaction. There are 2 options:

    • Left 'as is' but rename test method to `isConnectedOrConnectingReturnsTrueWhenInternetIsAvailable()
    • Add user interaction to make sure the data from internet was loaded successfully.
| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ How could I test if there is no internet connection? Thank you. \$\endgroup\$ – TheLearner Aug 3 '17 at 23:59
  • \$\begingroup\$ Ok, this is pure unit test where you assume there is an internet. In this case just to rename to `isConnectedOrConnectingReturnsTrueWhenInternetIsAvailable()'. \$\endgroup\$ – Dmytro Maslenko Aug 4 '17 at 0:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.