# Decreasing Applitools Android Boilerplate with Kotlin and JUnit

DevFeed: [Decreasing Applitools Android Boilerplate with Kotlin and JUnit](<https://devfeed.tech/articles/decreasing-applitools-android-boilerplate-with-kotlin-and-junit-20360.md>)

Original publisher: [Read original article](<https://engineering.ziffmedia.com/decreasing-applitools-android-boilerplate-with-kotlin-and-junit-46c61b5e10ce?source=rss----d6bb34696ef5---4>)

Author: Ben Boral

Published: 2019-11-15T17:46:53Z

Content type: tutorial

Language: en

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

Topics: [Testing](<https://devfeed.tech/topics/testing.md>), [Boilerplate](<https://devfeed.tech/topics/boilerplate.md>), [Kotlin](<https://devfeed.tech/topics/kotlin.md>), [SDKs](<https://devfeed.tech/topics/sdks.md>), [App](<https://devfeed.tech/topics/app.md>)

Tags: [android](<https://devfeed.tech/tags/android.md>), [applitools](<https://devfeed.tech/tags/applitools.md>), [boilerplate](<https://devfeed.tech/tags/boilerplate.md>), [code](<https://devfeed.tech/tags/code.md>), [espresso](<https://devfeed.tech/tags/espresso.md>), [junit](<https://devfeed.tech/tags/junit.md>), [kotlin](<https://devfeed.tech/tags/kotlin.md>), [testing](<https://devfeed.tech/tags/testing.md>)

## AI overview

This tutorial shows how to reduce boilerplate in Applitools visual regression tests for Android by using Kotlin and JUnit. It describes encapsulating repeated setup and cleanup in a helper and JUnit rule so the test code is more concise and easier to maintain.

## Source excerpt

Background At RetailMeNot, we are excited by the promise of automating visual regression tests with Applitools. In this post, I'll show how to make writing Applitools tests for Android a cinch. We'll start with the official integration guide recommendations and then use JUnit and Kotlin features to make our tests concise and maintainable. This post assumes the reader is familiar with Applitools visual testing. Our Starting Point: lots of boilerplate code Applitools's official integration guide for Espresso recommends a test that looks approximately like the below code block. I'll explain each line of this snippet later in the article. But for now, notice the excessive setup code and cleanup code that's getting in the way of our actual test code. This boilerplate would need to be copied for every test. https://medium.com/media/c042e70c7b2c423afde35fc101bc6e31/hrefThe Solution: a much simpler test We can reduce the boilerplate to achieve the following test. By stripping away the setup and cleanup as much as possible, we are left with the kernel of the test. In doing so, the purpose of the test is much easier to comprehend. ApplitoolsHelper, on line 2, has encapsulated the setup and cleanup. This will make long-term maintenance easier, because when the Applitools APIs change, we'll only need to update them in the one location. https://medium.com/media/de1efe74c1f65542057b061fd3b4ad35/hrefThe Required Steps to Run an Applitools Test Before diving into the helper code that enables our concise test, let's review the steps involved in an Applitools visual test: val eyes = Eyes() eyes.apiKey = "YOUR_API_KEY" eyes.appName = "YOUR_APP_NAME" We must initialize the Applitools SDK and set our API key and app name. This is going to be the same for every test in our suite. We will extract this code into a JUnit test rule. eyes.open("TEST_NAME"); This starts the Applitools test session. This must be called with a unique test name for each test. We will use a lambda expression, expl