Tricky parts when implementing unit tests for Android in Kotlin

Testing data classes in Kotlin As we all know, data classes are final by default on Kotlin and if you try to use Mockito and mock such a class, you will get an exception. What you need to do is add this file:

November 12, 2020 · 3 min · gmirchev90

Running unit tests for LiveData with Jupiter on Android

Running unit tests for ViewModels on Android where you have LiveData objects can be tricky. It is very possible that you get this exception: java.lang.RuntimeException: Method getMainLooper in android.os.Looper not mocked.

November 12, 2020 · 1 min · gmirchev90

Race conditions in unit tests with RxJava when using TestScheduler

I recently had a very strange bug when it comes to using a BehaviorSubject inside the object that I am testing.

August 12, 2020 · 2 min · gmirchev90

Creating a SonarQube custom plugin for Kotlin

In Android we decided that we want to implement a unit test performance monitor. The reason for it is that sometimes some unit tests would execute in more than 1 second and it was all because we would use the Schedulers.trampoline() RxJava scheduler instead of the TestScheduler which is part of the RX package.

May 15, 2020 · 6 min · gmirchev90

Android Notification White Square Icon

We have all seen the white square at the top type of a notification in the notification bar.

May 5, 2020 · 1 min · gmirchev90

Android Studio Live Templates

I was really wondering how I can remove repetitive tasks from Android Studio by assigning shortcuts or tags and I found that IntelliJ has Live Templates which I bet that little programmers use. So as an example I wanted when I type the word “tag” within a class the following to be autocompleted:

May 4, 2020 · 1 min · gmirchev90

Android adb server version (32) doesn’t match this client (Genymotion)

Probably you have seen this type of error when using Genymotion as the Android emulator on which you test your device. This is how the error looks like: adb server version (32) doesn’t match this client (35); killing… error: could not install *smartsocket* listener: Address already in use ADB server didn’t ACK \* failed to start daemon * There is one simple thing that worked for me. I opened the Settings menu of Genymotion, then ADB tab and selected: Use custom android SDK tools and I set the path to the tools. By default Genymotion uses its built-in tools. After that killing all working emulators and starting them again makes everything work. ...

May 4, 2020 · 1 min · gmirchev90

Card.io issue not supporting Samsung S5 and other devices

card.io 5.4.2 Card.io is a card recognition SDK used by PayPal which allows you to easily scan your own credit/debit cards and get information about them in the code. Problem Using card.io from the source and not from Maven requires you to have the latest Android NDK with which the project will be built. But compiling it with the latest NDK introduces a bug which prevents the library from supporting several phones like Samsung S5, Honor 7 and others.

May 4, 2020 · 1 min · gmirchev90

Mockito anyString() vs any() – null handling

Sometimes, when I mock a method in Mockito, I used anyString instead of any and I get into a mess and my test fails. Why? given(mUserRepo.getUser(anyString(). any())).willReturn(new User()); And what will happen if mUserRepo.getUser(null); UserRepo is called with null? Well, the matcher doesn’t work and the test fails. anyString() does not work with null values. If you pass null to a mocked service then use any() as a matcher.

May 4, 2020 · 1 min · gmirchev90

Context in Android Databinding XML files

It is so easy guys, just don’t forget that DataBinding creates a field called context which you can access by default in your XML file. So you can write something like this: <data> <variable name="name" type="String"/> </data> <layout> <TextView app:text="@{StringUtils.format(context, name)}" /> </layout> And it will be just fine. You have the context from the parent view. Check it HERE.

May 4, 2020 · 1 min · gmirchev90