Created
June 11, 2015 13:51
-
-
Save vgonda/1b2520619052cc5bf9b8 to your computer and use it in GitHub Desktop.
Example of how to use espresso-intents in Android tests
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
It was helpful. Thanks