Reading Time: 4 minutes

In the name of GOD

Hello everyone, thanks for coming back to my blog.

For this post, I thought of sharing a small experience with Kotlin. One day I was writing a test for our Android app that I stumbled upon a little funny bug.

I was writing tests and running them on API level 27 Android Emulator. Everything was good,tests were green and I was happy. After writing a few I decided to give it a shot on  lower API versions. That’s when I saw a mysterious test failing. To give you an idea I reproduced it for you guys :

The first thing that came to my mind was that some values do not match in assertions. Since I’m not a Test debug expert I decided to run the test again with a debugger attached. What I noticed was interesting to me. My test code did not even reach the forEach block! (weird!)

Here comes Sherlock Holmes!

First I replaced forEach block with a normal for loop and surprisingly everything worked perfectly. 

So my best guess is that something is wrong with this foreach function! Let’s uncover the hidden world and see how this function has been implemented:


Wait a minute that’s not Kotlin! As you can see it’s a normal Java method but what is wrong with this method? If you look closely you can see that this method has been added in Java 8. (aha) so this method is a Java 8 feature and all Java 8 features (except sugar) have been added to Android since version 27! And will crash on lower versions. 

Great now we know what the problem is and can start fixing it. The first thing one might do is simply just copy the implementation of foreach method in our test!! (not me!)

That looks exactly the same as for loop above! Ouch, I really don’t like that. Why are we even talking about Java in a world of Kotlin! There should be something similar in Kotlin! Let’s look in Koltin extension functions for Maps.

Oh, look there! There is a foreach function. It works with Map.Entry so let’s change our Test foreach argument to a single one.

Great! now we have fixed our issue and our test pass again. But we can do better here for the final result lets destructure Entry class to it’s property in our foreach

Awesome now we can write less code to assert them. Doesn’t it look familiar to you? It’s almost the same as how we started in the first place! The only difference is the curly braces. 

Now when I look at it seems kind of silly to me! I could minimize all this hassle with just curly braces?

Bonus :

1- IntelliJ Idea and Android Studio will highlight extension functions to yellow by default. So our first impl was not yellow and we could start our doubts right from the beginning and save some build time here! 

2- the new version of Android Studio will show a warning if we use Java foreach mistakenly.

3- in this last snippet I gathered all the approaches at one place. just remember this test works only on API version 24 and above. thus on lower versions first foreach will cause the test to crash. (JVM tests are all fine).

So this it guys. Hope you liked it. Happy coding and as always any feedback is appreciated.



Please enter your comment!
Please enter your name here