= new ActivityTestRule<>(MainActivity.class); @Test public void findViewPerformActionDone() { } } @Test public void testWithBoundService() { IBinder binder = serviceRule.bindService( new Intent(InstrumentationRegistry.getTargetContext(), MyService.class)); MyService service = ((MyService.LocalBinder) binder).getService(); assertTrue("True wasn't returned", service.doSomethingToReturnTrue()); } @Rule public final ServiceTestRule serviceRule = new ServiceTestRule();
a phone number into the dialer edit text field and presses the call button. onView(withId(R.id.edit_text_caller_number)) .perform(typeText(VALID_PHONE_NUMBER), closeSoftKeyboard()); onView(withId(R.id.button_call_number)).perform(click()); // Verify that an intent to the dialer was sent with the correct action, phone // number and package. Think of Intents intended API as the equivalent to Mockito's verify. intended(allOf( hasAction(Intent.ACTION_CALL), hasData(INTENT_DATA_PHONE_NUMBER), toPackage(PACKAGE_ANDROID_DIALER))); }
a phone number into the dialer edit text field and presses the call button. onView(withId(R.id.edit_text_caller_number)) .perform(typeText(VALID_PHONE_NUMBER), closeSoftKeyboard()); onView(withId(R.id.button_call_number)).perform(click()); // Verify that an intent to the dialer was sent with the correct action, phone // number and package. Think of Intents intended API as the equivalent to Mockito's verify. intended(allOf( hasAction(Intent.ACTION_CALL), hasData(INTENT_DATA_PHONE_NUMBER), toPackage(PACKAGE_ANDROID_DIALER))); }
a phone number into the dialer edit text field and presses the call button. onView(withId(R.id.edit_text_caller_number)) .perform(typeText(VALID_PHONE_NUMBER), closeSoftKeyboard()); onView(withId(R.id.button_call_number)).perform(click()); // Verify that an intent to the dialer was sent with the correct action, phone // number and package. Think of Intents intended API as the equivalent to Mockito's verify. intended(allOf( hasAction(Intent.ACTION_CALL), hasData(INTENT_DATA_PHONE_NUMBER), toPackage(PACKAGE_ANDROID_DIALER))); }
all Intents to ContactsActivity to return VALID_PHONE_NUMBER. Note that the Activity // is never launched and result is stubbed. intending(hasComponent(hasShortClassName(".ContactsActivity"))) .respondWith(new ActivityResult(Activity.RESULT_OK, ContactsActivity.createResultData(VALID_PHONE_NUMBER))); // Click the pick contact button. onView(withId(R.id.button_pick_contact)).perform(click()); // Check that the number is displayed in the UI. onView(withId(R.id.edit_text_caller_number)) .check(matches(withText(VALID_PHONE_NUMBER))); }
all Intents to ContactsActivity to return VALID_PHONE_NUMBER. Note that the Activity // is never launched and result is stubbed. intending(hasComponent(hasShortClassName(".ContactsActivity"))) .respondWith(new ActivityResult(Activity.RESULT_OK, ContactsActivity.createResultData(VALID_PHONE_NUMBER))); // Click the pick contact button. onView(withId(R.id.button_pick_contact)).perform(click()); // Check that the number is displayed in the UI. onView(withId(R.id.edit_text_caller_number)) .check(matches(withText(VALID_PHONE_NUMBER))); }
all Intents to ContactsActivity to return VALID_PHONE_NUMBER. Note that the Activity // is never launched and result is stubbed. intending(hasComponent(hasShortClassName(".ContactsActivity"))) .respondWith(new ActivityResult(Activity.RESULT_OK, ContactsActivity.createResultData(VALID_PHONE_NUMBER))); // Click the pick contact button. onView(withId(R.id.button_pick_contact)).perform(click()); // Check that the number is displayed in the UI. onView(withId(R.id.edit_text_caller_number)) .check(matches(withText(VALID_PHONE_NUMBER))); }
Elegant?? Unit Testing - Pablo Guardiola Android unit and instrumentation tests- Lars Vogel http://www.vogella.com/tutorials/AndroidTesting/article.html http://developer.android.com/intl/es/tools/testing-support-library/index.html