Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Transforming your Tests with Kotlin

Transforming your Tests with Kotlin

We know we should write tests, and we know that Kotlin gives us some nice features to use in our code, so how can we put these two together? There are some really great ways that Kotlin is being used in test writing, and we'll look at a number of them. This will include features such as higher order functions and escaping back-ticks and how they can make your tests more expressive. We'll also look at some testing libraries that take advantage of Kotlin's features. At the end of this talk you'll walk away with an understanding of how you can use Kotlin to make your tests more enjoyable.

Victoria Gonda

June 26, 2019

More Decks by Victoria Gonda

Other Decks in Programming


  1. Victoria Gonda • Android Engineer at Buffer • Author and

    Editor at RayWenderlich.com • Hedgehog and cat owner • she/her
  2. Higher order functions fun verifyLinkOpen(expectedUrl: String, openLink: () -> Unit)

    { Intents.init() try { openLink() intended(allOf(hasAction(ACTION_VIEW), hasData(expectedUrl))) } finally { Intents.release() } }
  3. Screen Robots class CalculatorScreenRobot : ScreenRobot<CalculatorScreenRobot>() { fun verifyTipIsCorrect(tip: String):

    CalculatorScreenRobot { return checkViewHasText(R.id.textTip, tip) } fun inputCheckAmountAndSelectOkayButton(input: String): CalculatorScreenRobot { return enterTextIntoView(R.id.inputAmount, input) .clickOkOnView(R.id.buttonOkay) } }
  4. Combining multiple features inline fun <reified T : ScreenRobot<*>> robot(block:

    T.() -> Unit) { T::class.java.newInstance().block() } robot<CalculatorScreenRobot> { inputCheckAmountAndSelectOkayButton("11") verifyTipIsCorrect("1.98") }
  5. Data classes data class UpdateData( var shareMode: ShareMode = ShareMode.ADD_TO_QUEUE,

    var media: MediaEntity? = null, var extraMedia: Array<MediaEntity>? = null, var retweet: RetweetEntity? = null, @Nullable var scheduledAt: Long? = null, var text: String = "", var facebookText: String = "", var profileIds: List<String>? = null, var subProfileIds: List<String>? = null, var sourceUrl: String? = null, var importId: String? = null, var shareDetails: ShareDetails? = null, var userChangedMedia: Boolean = false, var isEditing: Boolean = false, var editingProfile: ProfileEntity? = null, var networks: List<String>? = null, var facebookTags: List<FacebookTag>? = null, var id: String? = "", var lastEditedDate: Long? = 0, var editingProfileTimezone: String? = null, val location: Location? = null, val commentEnabled: Boolean = false, val commentText: String? = null )
  6. Data classes UpdateData(null, null, emptyArray(), null, 0, "Hello World", "",

    emptyList(), emptyList(), null, "buffer.com", null, false, true, null, emptyList(), null, "", 0, null, null, false, "")
  7. Kakao class SearchScreen : Screen<SearchScreen>() { val searchButton = KButton

    { withId(R.id.searchButton) } val snackbar = KView { withId(com.google.android.material.R.id.snackbar_text) } }
  8. Spek class ProfileSpec : Spek({ given("twitter profile") { on("validate image

    count") { it("should be valid for four images") { } it("should fail for ten images") { } } } })
  9. Spek Spek:ProfileSpec:given twitter profile:on validate image count:it 
 should be

    valid for four images
 => java.lang.AssertionError: Expected <true>, actual <false>.