Skip to main content


Showing posts from October, 2019

From Gson to Moshi, what I learned

There is no doubt that people are getting away from GSON and I agree with those reasons too. The only advantage GSON has over other parsing libraries is that it takes a really short amount of time to set up. Furthermore, the most important thing is that Moshi is embracing Kotlin support.

First let's implement the dependency:
implementation("com.squareup.moshi:moshi:1.8.0") It's not a struggle to migrate to Moshi. It's really Gson look-a-like. The only thing to do is annotate the object with @field:Json instead of @SerializedName (which is Gsons way for JS representation):

data class User( //GSON way @SerializedName("name") val name: String, @SerializedName("user_name") val userName: String, @SerializedName("last_name") val lastName: String, @SerializedName("email") val email: String ) data class User( //Moshi way @field:Json(name = "name") val name: String, @field:Json(name = "user_name…

Unit testing with coroutines

The coroutines API has already brought some innovation in the Android and Kotlin world. I always loved the idea of keeping it as simple as we all can. There is a saying around here that "Whoever talks to much, makes too much mistakes" and I see this a little bit related to Java's verbosity and also in the world of concurrency. It's said over and over again that concurrency is not simple and I couldn't agree more: You have to care about context, jobs running in parallel, cancelation, returning values etc.

I hope I gave my best in one of my previous articles explaining Kotlin Coroutines, therefore I will cover the testing tool of them today.

As usual, some might still fear testing, but I really find it so entertaining. But there is no new concept to add to software testing when talking about coroutines, except just defining a "default" TestCoroutineDispatcher which is only a CoroutineDispatcher which runs immediate and lazy code the same way. In other wo…

Room and coroutines testing

My last article covered some simple example about Room and RxJava instrumentation testing code. Coroutines also have great support in unit testing even though todays topic has nothing to do with it.  What I mean is that we are not going to cover runBlockingTest this time. Android doesn't support that (correct me if I'm wrong please) yet. However, I could schedule a topic about that because it really makes me excited. You can also check this awesome talk from Sean McQuillan, which I found pretty helpful.

Comparing to the last gists, we will try to jump from Rx to coroutines (without implementation) and write test class about it.  Instead of RxJava components, we can just mark methods as suspended:

This looks easier (or better say less confusing).

Two small notes that might be needed: 
Looking the code in the previous article, notice that you won't be needing the InstantTaskExecutorRule() and suddenly we won't be running the queries on the Main thread. That's because…

Room and RxJava testing

&nbps; When it comes to testing the data layer, we should always have time for that. It's very important. You lose data, you lose users. Since I really found testing + Room pretty amusing, I thought I should share it with you.

Room has already a great support for RxJava or Coroutines. I have used both ways to access the data layer and I was really satisfied with both. So I decided to make a 2-series blog posts with testing in Room with RxJava and testing Room with coroutines.

This is not a Rx-Coroutines comparison. 

I will start with Rx first. First of all this is the dependency for Rx support:

    androidTestImplementation “android.arch.core:core-testing:1.0.0-alpha5”

 So let's suppose we have this DAO:

I will skip the implementation of TypeConverters, but the important part is to notice that Room supports Date() objects as part of table column. Notice also that the annotation @VisibleForTesting is a pretty cool one. If I use that method outside my testing environment, the…