.st0{fill:#FFFFFF;}

Kotlin! Got Any Interesting Language/Testing Tips for Me? 

 October 29, 2019

by Jon Reid

6 COMMENTS

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?

Code snippet sample

Improve your test writing “Flow.”

Sign up to get my test-oriented code snippets.

Jon Reid is learning Kotlin. Is this the apocalypse?

Click to Tweet

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.

Jon Reid

About the author

Programming was fun when I was a kid. But working in Silicon Valley, I saw poor code lead to fear, with real human costs. Looking for ways to make my life better, I learned about Extreme Programming, including unit testing, test-driven development (TDD), and refactoring. Programming became fun again! I've now been doing TDD in Apple environments for 20 years. I'm committed to software crafting as a discipline, hoping we can all reach greater effectiveness and joy. Now a coach with Industrial Logic!

  • 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!

    • 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.

    • 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.

  • {"email":"Email address invalid","url":"Website address invalid","required":"Required field missing"}
    >