This week, I’m learning Kotlin. And what better way to learn a new language than by writing unit tests against the code?
What’s going on, you ask? Is the world coming to an end?

Improve your test writing “Flow.”
Sign up to get my test-oriented code snippets.
Jon Reid is learning Kotlin. Is this the apocalypse?
Company Benefit: A Week to Learn Something New
By day, I currently work for American Express, on the Amex iOS app. (Views expressed here are my own and do not necessarily reflect the views of my employer.)
And one of the benefits of working at Amex is, I get a full week to learn something new. (Work-related, of course! It can’t be “Best Beaches of Hawaii.”)
So, I’m going to learn Kotlin, the language of my Android cousins. (Just Kotlin at this point, no Android.)
But me being me, I want to use a test-centric approach. Unit tests can’t tell me if I’m writing “Kotliny” code. But they can tell me if the code works.
Update: You can watch a recording of me live-coding this.
Help Me with Your Kotlin Tips
Do any of you have Kotlin experience? If so, I’d like to hear from you. What are your tips & tricks? Are there any unit test gotchas? Is there anything like SwiftLint that can give me guidance about idioms? What’s the tooling like?
Help a brother out, leave a comment below.
Test gotchas: Default-final without @testable escape hatch. There are various tools that munge the bytecode to work around that without requiring you to open up everything at the source code level, e.g. DexOpener or PowerMock.
Fun tools:
– Mocking: https://mockk.io/ or https://github.com/nhaarman/mockito-kotlin
– Assertions / matchers: https://github.com/robstoll/atrium#how-is-atrium-different-from-other-assertion-libraries https://strikt.io/ . Sadly power-assert hasn’t made it over yet (as in Groovy originally https://groovy-lang.org/testing.html#_power_assertions and later ported to Scala https://github.com/pniederw/expecty).
– Test runners: https://junit.org/junit5/ of course, but also https://github.com/kotlintest/kotlintest which is a “batteries-included” test tool that reminds me of Jest in the JS world.
– Property-based testing: have a look at https://github.com/quicktheories/QuickTheories and https://jqwik.net/. The latter seems to have improved since I last looked at it. (Check https://propertesting.com/ for background reading.)
Linting:
– General Kotlin: https://ktlint.github.io/
– RxLint: https://bitbucket.org/littlerobots/rxlint/src/default/
Tooling:
– Even if you’re not targeting Android, Android Studio is basically a free first-class Kotlin IDE. The stream debugger is pretty neat: https://plugins.jetbrains.com/plugin/10301-kotlin-sequence-debugger It’s now bundled with the Kotlin plugin, but see the link and click “more” for a walkthrough with screenshots.
Final by default, eh? Coming from Swift (where our team adds final on everything we can), it doesn’t seem too bad. But Swift is also making me forget how good tooling can be. Does Kotlin’s final shut out tools that use reflection?
Thanks for the links!
Follow Kotlin idioms : https://kotlinlang.org/docs/reference/idioms.html.
Check bytecode generated by decompile to see the java code generated, many-a-times you can further optimize your code.
also, default `no` modifier is public in Kotlin, beware.
Hi Puru! Thanks, I’ll check out the idioms.
Your “beware” statement — I assume you mean nothing in Kotlin is public unless you say so? Then that’s the same as Swift.
A colleague found this tip and I love it; it makes for very readable test method names like Quick.
> Put the test method name in backticks. This allows spaces in the method name which highly improves the readability. This way, we don’t need an additional @DisplayName annotation.
Quoted from the following whicch has a lot of other great tips:
https://phauer.com/2018/best-practices-unit-testing-kotlin/
Ben, thanks for the link.
And yes, the back-tick trick… My initial thought was, “This is just syntactic sugar. Will it make any difference?” But I was amazed at how it _felt_ different immediately. It’s especially nice with anything that shows the names of the tests in a list.