# Intents

Artifact: [androidx.test.espresso:espresso-intents](https://developer.android.com/jetpack/androidx/releases/test-espresso) [View Source](https://cs.android.com/search?q=file:androidx/test/espresso/intent/Intents.java+class:androidx.test.espresso.intent.Intents)

*** ** * ** ***


```
public final class Intents
```

<br />

*** ** * ** ***

Intents enables validation and stubbing of intents sent out by the application under test.

An example test that simply validates an outgoing intent:

```
public void testValidateIntentSentToPackage() {
  // User action that results in an external "phone" activity being launched.
  user.clickOnView(system.getView(R.id.callButton));

  // Using a canned RecordedIntentMatcher to validate that an intent resolving
  // to the "phone" activity has been sent.
  intended(toPackage("com.android.phone"));
}
```

An example test with intent stubbing:

```
public void testActivityResultIsHandledProperly() {
  // Build a result to return when a particular activity is launched.
  Intent resultData = new Intent();
  String phoneNumber = "123-345-6789";
  resultData.putExtra("phone", phoneNumber);
  ActivityResult result = new ActivityResult(Activity.RESULT_OK, resultData);

  // Set up result stubbing when an intent sent to "contacts" is seen.
  intending(toPackage("com.android.contacts")).respondWith(result));

  // User action that results in "contacts" activity being launched.
  // Launching activity expects phoneNumber to be returned and displays it on the screen.
  user.clickOnView(system.getView(R.id.pickButton));

  // Assert that data we set up above is shown.
  assertTrue(user.waitForText(phoneNumber));
}
```

## Summary

| ### Public methods |
|---|---|
| `static void` | `https://developer.android.com/reference/androidx/test/espresso/intent/Intents#assertNoUnverifiedIntents()()` Asserts that Intents does not have any unverified intents. |
| `static https://developer.android.com/reference/java/util/List.html<https://developer.android.com/reference/android/content/Intent.html>` | `https://developer.android.com/reference/androidx/test/espresso/intent/Intents#getIntents()()` Returns the list of captured intents. |
| `static void` | `https://developer.android.com/reference/androidx/test/espresso/intent/Intents#init()()` Initializes Intents and begins recording intents. |
| `static void` | `https://developer.android.com/reference/androidx/test/espresso/intent/Intents#intended(org.hamcrest.Matcher<android.content.Intent>)(https://junit.org/junit4/javadoc/latest/org/hamcrest/Matcher.html<https://developer.android.com/reference/android/content/Intent.html> matcher)` Asserts that the given matcher matches one and only one intent sent by the application under test. |
| `static void` | `https://developer.android.com/reference/androidx/test/espresso/intent/Intents#intended(org.hamcrest.Matcher<android.content.Intent>,androidx.test.espresso.intent.VerificationMode)(https://junit.org/junit4/javadoc/latest/org/hamcrest/Matcher.html<https://developer.android.com/reference/android/content/Intent.html> matcher, https://developer.android.com/reference/androidx/test/espresso/intent/VerificationMode verificationMode)` Asserts that the given matcher matches a specified number of intents sent by the application under test. |
| `static https://developer.android.com/reference/androidx/test/espresso/intent/OngoingStubbing` | `@https://checkerframework.org/api/com/google/errorprone/annotations/CheckReturnValue.html https://developer.android.com/reference/androidx/test/espresso/intent/Intents#intending(org.hamcrest.Matcher<android.content.Intent>)(https://junit.org/junit4/javadoc/latest/org/hamcrest/Matcher.html<https://developer.android.com/reference/android/content/Intent.html> matcher)` Enables stubbing intent responses. |
| `static void` | `https://developer.android.com/reference/androidx/test/espresso/intent/Intents#release()()` Clears Intents state. |
| `static https://developer.android.com/reference/androidx/test/espresso/intent/VerificationMode` | `https://developer.android.com/reference/androidx/test/espresso/intent/Intents#times(int)(int times)` Allows verifying a specific number of intents sent by the application under test. |

## Public methods

### assertNoUnverifiedIntents

```
public static void assertNoUnverifiedIntents()
```

Asserts that Intents does not have any unverified intents. You can use this method after you have verified your intents to make sure that nothing unexpected was sent out. This is an equivalent of verifyNoMoreInteractions() in Mockito.

### getIntents

```
public static List<Intent> getIntents()
```

Returns the list of captured intents. Intents are recorded from the time that Intents.init is called.

Callers can then verify the list of captured intents using their choice of assertion framework, such as [truth](http://google.github.io/truth).

### init

```
public static void init()
```

Initializes Intents and begins recording intents. Must be called prior to triggering any actions that send out intents which need to be verified or stubbed. This is similar to MockitoAnnotations.initMocks.

### intended

```
public static void intended(Matcher<Intent> matcher)
```

Asserts that the given matcher matches one and only one intent sent by the application under test. This is an equivalent of verify(mock, times(1)) in Mockito. Verification does not have to occur in the same order as the intents were sent. Intents are recorded from the time that Intents.init is called.

| Parameters |
|---|---|
| `https://junit.org/junit4/javadoc/latest/org/hamcrest/Matcher.html<https://developer.android.com/reference/android/content/Intent.html> matcher` | the `https://junit.org/junit4/javadoc/latest/org/hamcrest/Matcher.html` to be applied to captured intents |

| Throws |
|---|---|
| `junit.framework.AssertionFailedError` | if the given `https://junit.org/junit4/javadoc/latest/org/hamcrest/Matcher.html` did not match any or matched more than one of the recorded intents |

### intended

```
public static void intended(Matcher<Intent> matcher, VerificationMode verificationMode)
```

Asserts that the given matcher matches a specified number of intents sent by the application under test. This is an equivalent of verify(mock, times(num)) in Mockito. Verification does not have to occur in the same order as the intents were sent. Intents are recorded from the time that Intents.init is called.

| Parameters |
|---|---|
| `https://junit.org/junit4/javadoc/latest/org/hamcrest/Matcher.html<https://developer.android.com/reference/android/content/Intent.html> matcher` | the `https://junit.org/junit4/javadoc/latest/org/hamcrest/Matcher.html` to be applied to captured intents |
| `https://developer.android.com/reference/androidx/test/espresso/intent/VerificationMode verificationMode` | the verification mode to use |

| Throws |
|---|---|
| `junit.framework.AssertionFailedError` | if the given `https://junit.org/junit4/javadoc/latest/org/hamcrest/Matcher.html` did not match the expected number of recorded intents |

### intending

```
@CheckReturnValue
public static OngoingStubbing intending(Matcher<Intent> matcher)
```

Enables stubbing intent responses. This method is similar to Mockito.when and is particularly useful when the activity launching the intent expects data to be returned (and especially in the case when the destination activity is external). In this case, the test author can call intending(matcher).thenRespond(myResponse) and validate that the launching activity handles the result correctly. **Note:** the destination activity will not be launched.

| Parameters |
|---|---|
| `https://junit.org/junit4/javadoc/latest/org/hamcrest/Matcher.html<https://developer.android.com/reference/android/content/Intent.html> matcher` | the `https://junit.org/junit4/javadoc/latest/org/hamcrest/Matcher.html` that matches intents for which stubbed response should be provided |

| Returns |
|---|---|
| `https://developer.android.com/reference/androidx/test/espresso/intent/OngoingStubbing` | `https://developer.android.com/reference/androidx/test/espresso/intent/OngoingStubbing` object to set stubbed response |

### release

```
public static void release()
```

Clears Intents state. Must be called after each test case.

### times

```
public static VerificationMode times(int times)
```

Allows verifying a specific number of intents sent by the application under test. This is an equivalent of times(num) in Mockito.

| Parameters |
|---|---|
| `int times` | the number of times that the intent should be matched. |