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:


Conclusion
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

What I learned from Kotlin Flow API

I used to check the docs and just read a lot about flows but didn't implement anything until yesterday. However, the API tasted really cool (even though some operations are still in Experimental state).Prerequisites: If you don't know RxJava it's fine. But a RxJava recognizer would read this faster.Cold vs Hot streamsWell, I really struggled with this concept because it is a little bit tricky. The main difference between cold and hot happened to be pretty simple: Hot streams produce when you don't care while in cold streams, if you don't collect() (or RxJava-s equivalent subscribe()) the stream won't be activated at all. So, Flows are what we call cold streams. Removing the subscriber will not produce data at all, making the Flows one of the most sophisticated asynchronous stream API ever (in the JVM world). I tried to make a illustration of hot and cold streams: Since I mentioned the word asynchronous this implies that they do support coroutines also. Flows vs…

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…