Kotlin and your (my?) favorite libraries to architect and test

Olivier Goutay
AndroidPub
Published in
3 min readMay 22, 2017

As the hype around Kotlin is pretty high, since Google announced its official support for Android development, I got a few questions concerning a few libraries I have on github that demonstrates how to use Espresso, Dagger, Mockito and such, all together.

The first part will show how to use Espresso + Mockito in Kotlin and the second part will add Dagger on top of it.

Espresso + Mockito in Kotlin

Alright so first thing to do, let’s add the mockito-kotlin dependency to the project. You are not forced to use it, but it adds some nice helper functions to use Mockito in Kotlin. The documentation is available here: https://github.com/nhaarman/mockito-kotlin

androidTestCompile 'com.linkedin.dexmaker:dexmaker-mockito:2.2.0'
androidTestCompile 'org.mockito:mockito-core:2.7.21'
androidTestCompile "com.nhaarman:mockito-kotlin:1.4.0"

The mocking code changed a little bit with Kotlin, so let’s look at it in one of my project (https://github.com/omadahealth/AndroidMVPDemo):

var movieSearchService = mock<MovieSearchService> {
on { getMovies(anyString(), anyString(), anyInt()) } doReturn Observable.error<SearchResults>(IOException())
}

Basically, the “on” replaces the “when” used usually by Mockito, and the remaining is self explanatory.
Also, the “verify” syntax remains the same :)
Please look at the example project if you want to compile/play with it.

Dagger + Espresso + Mockito in Kotlin

Now let’s take a look at how to use Dagger within your Kotlin project (example project is https://github.com/olivierg13/EspressoMockitoDaggerLeakCanary/tree/kotlin).

Kotlin has its own Annotation processor, called “kapt”. Let’s take a look at the dependencies:

//Mockito
androidTestCompile 'com.linkedin.dexmaker:dexmaker-mockito:2.2.0'
androidTestCompile 'org.mockito:mockito-core:2.7.21'
androidTestCompile "com.nhaarman:mockito-kotlin:1.4.0"
debugCompile "com.nhaarman:mockito-kotlin:1.4.0"

//Dagger
compile 'com.google.dagger:dagger:2.10-rc4'
kapt 'com.google.dagger:dagger-compiler:2.10-rc4'
provided 'javax.annotation:jsr250-api:1.0'

Note that you need to add “mockito-kotlin” as a debug dependency, since you need to use it with Dagger to inject Mock objects in debug.

I chose to store the Dagger AppComponent in the Application class, it might depend on your architecture, this choice is yours.

companion object {
@JvmStatic lateinit var daggerComponent: AppComponent
}

override fun onCreate() {
super.onCreate()
daggerComponent = DaggerAppComponent.builder()
.appModule(AppModule(false))
.build()

if (LeakCanary.isInAnalyzerProcess(this)) {
// This process is dedicated to LeakCanary for heap analysis.
// You should not init your app in this process.
return
}
LeakCanary.install(this)
}

And then a Module would look like:

@Module
class AppModule(val mockManagers: Boolean) {

@Provides
@Singleton
fun provideAccountManager(): AccountManager {
if (mockManagers) {
return mock {
on { accountFirstName() } doReturn "Mockito"
}
}
return AccountManager()
}
}

Very similar than what we have in Java, just adding the Kotlin syntax. Please look at the example project if you want to compile/play with it :)

Conclusion

With these examples, you can keep the best of both worlds: libraries like Dagger that allow you to have a nice injection architecture, Espresso and Mockito that allow you to write UI tests running in milliseconds, and finally Kotlin which is an awesome language.

With these, you are gonna be able to have a performant and clean architecture, and deploy super fast to the Play Store.

Note:

This is not an asset if you were used to play Ping Pong during release / testing phases :)

--

--

Olivier Goutay
AndroidPub

Senior Software Engineer @ Solana Labs | Ex-Netflix | Ex-Mentor at Code2040