特定のアクティビティを実行する場合は、 ActivityTestRuleまたはIntentTestRuleが必要です。
この契約は、最初のパラメータとして開始したい希望のアクティビティを設定するだけで簡単に説明できます。ここで注意することが重要
ActivityTestRule<>(ActivityYouWantToStart.class, initialTouchMode, launchActivity) //rule for activity start
IntentTestRule<>(ActivityYouWantToStart.class, initialTouchMode, launchActivity) //used for testing intents and activities
は
launchActivityブール値です。 これをtrueに設定すると、テストごとに自動的にアクティビティが起動されます。
その他の用途として、実行する前にいくつかの意図やデータなどを送ることが期待されるアクティビティがある場合は、このパラメータをに設定して、に設定し、テスト/アクティビティを実行する前にアプリを準備します。例えば
アクティビティBを実行するために、共有設定に保存されたユーザーでログインし、その活動の論理が言う必要がある場合:
if(!userIsLoggedIn()){
jumpToMainActivity();
}
その後、あなたはそのテストを実行する前に、あるいは、例えば、いくつかのモックユーザーを保存することができますデータベースをモックオブジェクトで埋める。アクティビティBのために環境を準備した後、すべてを呼び出す。
mActivityRule.launchActivity(null);
launchActivityはパラメータを取ることに注意してください、それはあなたがテストを開始する前にテントを準備することができる方法で、いくつかの余分なデータを想定した場合、これは、活動Bが受け取る実際にテントです。
カスタムアクティビティを実行しているの完全な例:
/**
* Testing of the snackbar activity.
**/
@RunWith(AndroidJUnit4.class)
@LargeTest
public class SnackbarActivityTest{
//espresso rule which tells which activity to start
@Rule
public final ActivityTestRule<SnackbarActivity> mActivityRule =
new ActivityTestRule<>(SnackbarActivity.class, true, false);
@Override
public void tearDown() throws Exception {
super.tearDown();
//just an example how tear down should cleanup after itself
mDatabase.clear();
mSharedPrefs.clear();
}
@Override
public void setUp() throws Exception {
super.setUp();
//setting up your application, for example if you need to have a user in shared
//preferences to stay logged in you can do that for all tests in your setup
User mUser = new User();
mUser.setToken("randomToken");
}
/**
*Test methods should always start with "testXYZ" and it is a good idea to
*name them after the intent what you want to test
**/
@Test
public void testSnackbarIsShown() {
//start our activity
mActivityRule.launchActivity(null);
//check is our text entry displayed and enter some text to it
String textToType="new snackbar text";
onView(withId(R.id.textEntry)).check(matches(isDisplayed()));
onView(withId(R.id.textEntry)).perform(typeText(textToType));
//click the button to show the snackbar
onView(withId(R.id.shownSnackbarBtn)).perform(click());
//assert that a view with snackbar_id with text which we typed and is displayed
onView(allOf(withId(android.support.design.R.id.snackbar_text),
withText(textToType))) .check(matches(isDisplayed()));
}
}
私は([リンク]示唆するかもしれないhttps://google.github.io/android-testing-support-library/docs/espresso/index.html )と[this one](https://developer.android.com/training/testing/ui-testing/espresso-testing.html)? – ddb
私はすでにこれらのリンクを見てきましたが、私の問題を助けてくれる具体的な情報は何ですか? – Moses
申し訳ありません@モーゼス、あなたの質問から、あなたがここに投稿する前にいくつかの解決策についてグーグルではっきりしていなかった – ddb