7 steps for fixing flaky tests

Every once in a while you might encounter a flaky unit test. I know, unit tests can’t be flaky by definition … but also life comes in the way, so sometimes it happens. Such a test will consistently pass on most developer machines, but fail in a CI environment every once in a while. In this post I’ll share how I go about fixing such problems.
read more

How to setup Jacoco for Android project with Java, Kotlin and multiple flavours

Jacoco is a widely used library to measure test code-coverage in JVM-based projects. Setting it up for Android applications has a few quirks and having multiple flavours, using Kotlin and writing (some) tests in Robolectric makes it even tricker. There are already great tutorials in how to set it up, like THIS and THIS one. In this post however I’ll not only give you a ready solution, but share all details how I got to it – this way you’ll be able to adapt it in the best way for your project.
read more

Use custom Hamcrest Matchers to level-up your tests

Ideally automated tests should be predictable, isolated and precise, allowing you to find an issue quickly. If these conditions are met, you’ll never have to change a test unless to accommodate a changed requirement. This sounds great on paper, but in practice we often forget the isolated bit and start testing multiple things in a single test. If abused, we’ll end up with tests that need updating all the time, causing developer frustration and wasting time.

One tool that can help with isolation of tests is using matchers. As the name suggests, matchers allow you to match an object agains certain conditions.
read more

Updating Mockito to mock Kotlin classes

With the exponentially increasing usage of Kotlin these days, many developers face the issue of how to test the newly created Kotlin classes. As we know all classes and methods are final be default in Kotlin, unless specifically open-ed. Unfortunately Mockito, one of the most popular mocking libraries for Java projects, can’t easily mock final classes. Since we don’t want to open up everything just for testing purposes, we need another solution.

Hadi Hariri highlighted in his excellent blog post that Mockito version 2.1.0 and above can perform the magic of mocking final classes. Since mocking is something used only in tests … and usually it just works, we’ve neglected Mockito and were still using a very outdated version (1.10.19) in our project. There were a few pain-points while updating to the latest one, so hopefully this post will save you some time when going through the same process.
read more

Running Android unit / instrumentation tests from the console

Unit tests

Here’s a few handy commands if you want to run only a specific unit test(s). Suppose we have the following unit tests in the project:
unit_tests

./gradlew test – run unit tests for all variants
./gradlew testDebug – run tests for Debug variant
./gradlew testDebug --tests="*.helpers.*" – run all tests in the helpers package
./gradlew testDebug --tests="*.HelperTest" – run all tests in HelperTest.java class
./gradlew testDebug --tests="*.getHelp" – run only the getHelp test method.
read more