After being used to the traditional way of debug an android app by using mocking and interceptors, I came across an interesting problem with gRPC. I wanted to do the same. Basically, add an interceptor that mock a server response.
An important note: The app is in Kotlin, however this code I show is in Java. What I want to show here is that the concepts in gRPC are generally easily convertible to other languages. And even if you are actually coding in another supported language, you should be able to do pretty much the same
I didn’t find any way to send back a message to my client. After going through the gRPC code and especially the comments (ctrl+left click on android studio),something caught my eye:
Here was my solution: InProcessServerBuilder. After a little bit more searching in github, I found this:
The most important thing to note here is the use of the two 2 builders:
and the use of InProcessServerBuilder’s function :
And if you are familiar with the greeter example of gRPC, everything will make sense here. If you are not, you can join my course on Udemy (my class is C# one, but trust me it is similar to Java/Kotlin).
You can now adapt this solution for the testing part of you application !