# Note to Future Me -- Testing Intents with Matchers

DevFeed: [Note to Future Me -- Testing Intents with Matchers](<https://devfeed.tech/articles/note-to-future-me-testing-intents-with-matchers-32073.md>)

Original publisher: [Read original article](<https://www.maiatoday.net/p/note-to-future-me-testing-intents-with-matchers/>)

Published: 2018-01-26T14:54:45Z

Content type: tutorial

Language: en

Sources: [maiatoday](<https://devfeed.tech/sources/maiatoday.md>)

Topics: [Testing](<https://devfeed.tech/topics/testing.md>), [test](<https://devfeed.tech/topics/test.md>), [parcelable](<https://devfeed.tech/topics/parcelable.md>), [Code](<https://devfeed.tech/topics/code.md>), [Library](<https://devfeed.tech/topics/library.md>), [Kotlin](<https://devfeed.tech/topics/kotlin.md>)

Tags: [code](<https://devfeed.tech/tags/code.md>), [data](<https://devfeed.tech/tags/data.md>), [data-class](<https://devfeed.tech/tags/data-class.md>), [error-messages](<https://devfeed.tech/tags/error-messages.md>), [kotlin](<https://devfeed.tech/tags/kotlin.md>), [library](<https://devfeed.tech/tags/library.md>), [parcelable](<https://devfeed.tech/tags/parcelable.md>), [testing](<https://devfeed.tech/tags/testing.md>), [testing-matchers](<https://devfeed.tech/tags/testing-matchers.md>), [tests](<https://devfeed.tech/tags/tests.md>)

## AI overview

This tutorial demonstrates testing Android activity intents with Espresso and Hamcrest matchers in Kotlin. It passes a parcelable data object between activities, verifies the intent extra and its properties, and shows how custom matchers can improve error messages and test only selected properties.

## Source excerpt

I want to make my little experiments public and save them as a reminder for future me. So here is yet another post on Matchers and Espresso testing. I want to test this: First activity starts another activity at a button click. First activity passes a parcelable object to the second activity. Test that the intent that starts the second activity contains the object with the correct properties. I built a contrived example, MainActivity collects some info and passes this to StarActivity in a parcelable called ContrivedParams. See the sample code on GitHub. To write the tests I brushed up on Hamcrest[1][2][3]: a library that makes it easier to write readable tests. Instead of assert(expected==actual) you can write almost-english assertThat(actual, is(expected)) which is a sugar coated version of assertThat(actual is(equalto(expected))) Sadly though is is a hard keyword in Kotlin so I ended up using the isA() and equalTo() varieties of calls rather than escaping is with backticks. Another reason to use Hamcrest -- better error messages. Compare these two messages. java.lang.AssertionError at net.maiatoday.hellointentmatcher.ContrivedParamsTest.showErroMessagesTest(ContrivedParamsTest.kt:37) with the message given by a custom matcher java.lang.AssertionError: Expected: title should return "Hello World" but: was "Hello World!" at net.maiatoday.hellointentmatcher.ContrivedParamsTest.showErroMessagesTest(ContrivedParamsTest.kt:42) or the error message given by an object matcher java.lang.AssertionError: Expected: <ContrivedParams(title=Hello World, starCount=3, colour=#00b0ff)> but: was <ContrivedParams(title=Hello World!, starCount=4, colour=#00b0fff)> at net.maiatoday.hellointentmatcher.ContrivedParamsTest.showErroMessagesTest(ContrivedParamsTest.kt:47) The parameters for the second activity is passed in a parcelable data class. I used the Espresso-intents library . Start the activity with an IntentsTestRule. The IntentsTestRule initialises the Espresso intents before each