Skip to main content

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 IDE will yell at me.

Some things to keep in mind: Dao methods marked with @Query actually return a value. It could be a void or an integer holding the number of affected rows.

Notice that all my queries are returning some RxJava components. In case of @Insert or @Update or  @Delete you could also use a Single<Long> or a Maybe<Long> (where Long could be the datatype of the affected id, if you marked it as String or Int, put int in there). I just return a Completable. In my case I don't need the id of the affected row.

So, what to test? Well usually, the main parts are insertions, deletions or updating values. The selection queries can be seen as tools to verify the above operations, but that doesn't mean you can't individually test each selection, or change it with another one (must note that should insert some data first anw).

Note: This kind of tests require a device because they are instrumental tests.

Now the tests. The first thing to keep in mind is that we don't really want to use our real database because that would be a total disaster. So, Room provides an in-memory database helper for you to run your tests:

Since we are done with the configuration. First let's test the insertion:

Notice that we should add the blockingAwait() method behind the completable execution otherwise the selection might execute before insertion. And that's the only tricky part for testing Room queries with RxJava. Same logic applies to Update, Delete.

Check how RxJava has some awesome support for asserting values only by adding the test() method behind.

And the delete part:

Async operations are pretty hard as a concept. And I really think Rx is one of the best tools that has already given a good problem solving about it.
The one and only thing I don't really like about Google is that they suddenly dropped support for Rx. They are developing infinitely for coroutines and believe me, that's really great, but for example the viewModelScope should have had a viewModelCompositeDisposable as well (perhaps unrelated to current topic but really, that's what I think and this is my only article where Rxjava is included. I'm more "coroutine-oriented").

Stavro Xhardha

Popular posts from this blog

Modularizing your Android app, breaking the monolith (Part 1)

Inspired by a Martin Fowlers post about Micro Frontends, I decided to break my monolithic app into a modular app. I tried to read a little more about breaking monolithic apps in Android, and as far as I got, I felt confident to share my experience with you. This will be some series of blog posts where we actually try to break a simple app into a modularized Android app.

Note: You should know that I am no expert in this, so if there are false statements or mistakes please feel free to criticize, for the sake of a better development. 

What do you benefit from this approach:
Well, people are moving pretty fast nowadays and delivery is required faster and faster. So, in order to achieve this, modularising Android apps is really necessary.You can share features across different apps. Independent teams and less problems per each.Conditional features update.Quicker debugging and fixing.A feature delay doesn't delay the whole app. As per writing tests, there is not too much difference about…

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…

Modularizing your Android app, breaking the monolith (Part 2)

This is part 2 of a series of articles about modularizing Android app. If you haven't yet read the first article, you may find it here.

On our first article we just moved some classes outside the application and applied as an independent module. But what if we have dependencies pulled from the application level? This could be a small challenge. First of all, we want to clarify on how are we going to modularize the app. And depending on the previous article, I chose the by feature version of modularization. First of all, let's show some dependencies that are going to be needed in the whole app.

Note: I'm using Dagger for handling dependencies but manual DI or any dependency tool should be fine to understand this part.

So, this is my dependency schema:

Well, it's not that bad, but this isn't what we want to transform to when trying to modularize the app. If you think about it, modules that don't need a dependency, can get it quite easily. For example: A FeatureXVi…