I need to test one action in my android app. The ActivityInstrumentationTestCase2 documentation says:
This class provides functional testing of a single action.
And the ActivityTestRule documentation says:
This rule provides functional testing of a single action.
Almost the same words. In addition to the two samples that I encoded, do the same. So do I prefer an ActivityTestRule over an ActivityInstrumentationTestCase2 or vice versa?
I see that the extension ActivityInstrumentationTestCase2 looks like a test in the style of JUnit3 (its ancestor is junit.framework.TestCase , and testing methods should start with the word test ).
Using ActivityTestRule
package sample.com.sample_project_2; import android.support.test.rule.ActivityTestRule; import android.support.test.runner.AndroidJUnit4; import android.test.suitebuilder.annotation.LargeTest; import org.junit.Rule; import org.junit.Test; import org.junit.runner.RunWith; import static android.support.test.espresso.Espresso.onView; import static android.support.test.espresso.action.ViewActions.typeText; import static android.support.test.espresso.matcher.ViewMatchers.withId; @RunWith(AndroidJUnit4.class) @LargeTest public class ApplicationTest { @Rule public ActivityTestRule<SecAct> mActivityRule = new ActivityTestRule(SecAct.class); @Test public void foo() { onView(withId(R.id.editTextUserInput)).perform(typeText("SAMPLE")); } }
Extension ActivityInstrumentationTestCase2
package sample.com.sample_project_2; import android.test.ActivityInstrumentationTestCase2; import static android.support.test.espresso.Espresso.onView; import static android.support.test.espresso.action.ViewActions.typeText; import static android.support.test.espresso.matcher.ViewMatchers.withId; public class ApplicationTest2 extends ActivityInstrumentationTestCase2<SecAct> { public ApplicationTest2() { super(SecAct.class); } @Override protected void setUp() throws Exception { super.setUp(); getActivity(); } public void testFoo2() { onView(withId(R.id.editTextUserInput)).perform(typeText("SAMPLE 2")); } }
source share