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

Simple RxJava mistakes and how to avoid them

1. Observable creation and error handling

Consider the following example:

public Observable<Book> getFavoriteBook(User user) {
	return Observable.just(user.getFavoriteBookId())
                .flatMap(bookId -> bookService.getById(bookId))
                .onErrorReturn(throwable -> DEFAULT_FAVORITE_BOOK);
}

Focus on the error handling part. In my experience in 95% of the cases the expectation behind the statement .onErrorReturn(...); is to ensure that the method getFavoriteBook() is “safe”, e.g. that an exception cannot be thrown from it at all, as if it was surrounded by a giant try-catch.

read more

How to handle dynamic JSON response with Retrofit

Note: This post is based on the widely used Retrofit2 networking library. Although the examples use a Gson converter, the same concept can be used with most of the other supported ones as well.

Imagine you’re in a situation where your backend can return a JSON response that’s dynamic in nature, a.k.a some parts of it don’t adhere to a specific pre-defined schema. Say you retrieve information about a webpage that you need to open in a WebView. You need to support both GET and POST HTTP requests, so two valid responses are:
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

60 day running challenge

Today I took up a 60 day running challenge. It’s super simple – I need to run 60 consecutive days to succeed. I took up the challenge mainly for 2 reasons:

  • To give myself a lesson in persistence, which I truly believe is the most important factor to success in any field or undertaking.
  • Health benefits – I don’t expect to become a model, but should see some (minor) weight loss and boost in my overall energy levels and endurance.
  • Fun – experienced runners say running can be fun. Currently I don’t see how this can be true, but let’s put it to the test.

Well today I did get to Primrose Hill and backwards, so let’s say run 00 to be completed.
run00

Level-up your Android development by using a proxy tool

This post will list some of my favourite features of a Proxy tool that are used on a daily bases in my team. It’s about giving you the overview of how such a tool will help you be more efficient in your day-to-day development process. It won’t get into details of how to setup a proxy, how to use these features or which specific tool to use – this will be covered in a future post.

Let’s get to it!
read more

Percent Support Library – what, when, how?

What, when?

With the release of Android M (API 23) the Support library got a major update as well. One of the unsung heroes is a feature badly requested since Android’s early days – the ability to set dimensions in percentages. With the release of the Percent Support Library is’t now possible to set a View to take exactly 30% of the screen, or to set it’s marginTop to 10% for example.

read more

Basic Java: What Interfaces really are?

As an assistant of a course in basic Java in my local university, I saw a common misunderstanding among students what interfaces really are. In this short article I’ll try to give students another point of view.

As we all know “an interface is a group of related methods with empty bodies”*. Consider the following example:
read more