Skip to content

Instantly share code, notes, and snippets.

@vgonda
Created June 11, 2015 13:51
Show Gist options
  • Star 29 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save vgonda/1b2520619052cc5bf9b8 to your computer and use it in GitHub Desktop.
Save vgonda/1b2520619052cc5bf9b8 to your computer and use it in GitHub Desktop.
Example of how to use espresso-intents in Android tests
package com.collectiveidea.example;
import android.content.Intent;
import android.support.test.espresso.intent.Intents;
import android.support.test.rule.ActivityTestRule;
import android.support.test.runner.AndroidJUnit4;
import com.collectiveidea.example.ui.LoginActivity;
import com.collectiveidea.example.ui.MainActivity;
import com.collectiveidea.example.util.AccountUtility;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import static android.support.test.espresso.intent.Intents.intended;
import static android.support.test.espresso.intent.Intents.times;
import static android.support.test.espresso.intent.matcher.IntentMatchers.hasComponent;
@RunWith(AndroidJUnit4.class)
public class MainActivityTest {
@Rule
public final ActivityTestRule<MainActivity> rule =
new ActivityTestRule<>(MainActivity.class, true, false);
@Test
public void testWhenThereInNoExistingAccount_LaunchesLoginActivity() throws Exception {
Intents.init();
AccountUtility.removeAccounts();
rule.launchActivity(new Intent());
intended(hasComponent(LoginActivity.class.getName()));
Intents.release();
}
@Test
public void testWhenThereIsAnExistingAccount_DoesNotLaunchLoginActivity() throws Exception {
Intents.init();
AccountUtility.addAccount("email@example.com", "TOKEN");
rule.launchActivity(new Intent());
intended(hasComponent(MainActivity.class.getName()));
intended(hasComponent(LoginActivity.class.getName()), times(0));
Intents.release();
}
}
@JohnBrix
Copy link

very helpful thanks for this!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment