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

The difference between a spec and a test (Behaviour Driven Development)

What is a spec There was a time where I saw some tests which my colleagues made where the file name in the test folder would be: “users.spec.coffee”. So “spec” is short of specification and it is a totally different way of how we think about tests.

May 4, 2020 · 3 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

How to mock a dynamic value returned from a method with Mockito?

Probably, most of you already know this, but it is good to keep it in mind when you want to dynamically change the values returned by a mock you have.

May 4, 2020 · 1 min · gmirchev90

Mockito verify input parameter was called with a given value

Edit 23.04.2018 According to this issue, Mockito cannot capture var-args still. There is a very easy way to verify a method has been called with the right parameter in Mockito. All you need to do is use captors, to capture the actual argument. Here is how you can use them: ArgumentCaptor argument = ArgumentCaptor.forClass(RuleEntity.class); verify(mRulesRepository).createRule(argument.capture()); assertEquals(1,argument.getValue().getRules().get(0).getTriggers().size()) Simple it is, just a quick reminder for starters.

May 4, 2020 · 1 min · gmirchev90