Mocking a server with Firebase

Rudy Sonetti
ProAndroidDev
Published in
6 min readFeb 2, 2019

--

Nowadays, it’s very important to test an app functionalities. To do this, the easiest way is to create fake and static contents as models, automate some user actions and then assert the UI result with Espresso.

It is pretty useful but what happen if our user actions contain some server calls? We can’t assert dynamic data.

“You just have to create an API endpoint on a server that sends fake and static contents”

Right, but we’re Android developers not backend developers, however Google is.

Indeed, Google provides some services like Firebase that allow us to build a backend without worrying about the hosting, the configuration, the security and moreover, it is mostly free.

That is why, in this article, I will show a little example on how to mock a server with Firebase Cloud Functions to speed up the UI tests set up.

Create the Firebase project

Admitting that I want to test an app that retrieves a Github user information. So I will mock the Github API endpoint which can do that by sending static information.

First, we need to go on the Firebase console and create a project.

The Firebase project created, it’s time to initiate the project files.

To do this, we need the package manager npm. So let’s check if it is installed on the computer by running this command in a terminal.

If the command is “not found”. Go the NPM website and click on “Download Node.js and npm”.

Now that we have npm installed, we can run these commands :

> npm install -g firebase-tools #install the firebase tools> mkdir MockGithub #create a directory> cd MockGithub #navigate to this directory> firebase init #initiate the server project

A few questions will be asked. In the first one, we have to select “Functions” with spacebar and then click Enter to continue.

Answer to the other questions by selecting your Firebase project that you created before, choosing Javascript/Typescript language as you prefer, enabling ESLint to initiate default files and installing dependancies.

After few log messages, it should end with a success message. A firebase.json and a functions/ directory are generated in the current directory.

Let’s check the response pattern from the real Github REST API with CURL or an API tester client like Insomnia to be able to replicate it.

I try to find my github account information by calling the URL :

https://api.github.com/users/:login

We can see that the response is a simple JSON object with some fields. With our mock endpoint, we will just send an id, login and a name as example.

Let’s first install express in the project to custom our routes (don’t forget the save option to automatically add it in the list of dependancies in package.json).

> npm install express --save

You can now open the functions/index.js file with your favorite code editor and configure your routes like the example below.

In the code above, my server listens the URL /users/:login with a user’s login as parameter and sends back a JSON object for two different mocked users or a 404 Error otherwise.

Now we can deploy our endpoint with the command below.

> firebase deploy

Once the deployment is completed, the API endpoint URL is displayed. For me it’s https://us-central1-mockgithub-18fe8.cloudfunctions.net/users/:login

We can now test our API endpoint.

Test the Android app

To test the new created endpoint in the same conditions, we have to separate the real API version from our mock version.

Inside the android{} tag in the module build.gradle, let’s add two different product flavors with a variable that contains the server domain.

flavorDimensions "server"
productFlavors {
realGithub {
buildConfigField "String", "urlServer", "\"api.github.com\""
dimension "server"
}
mockGithub {
buildConfigField "String", "urlServer", "\"us-central1-mockgithub-18fe8.cloudfunctions.net\""
dimension "server"
}
}

Don’t forget to replace this mock URL with yours.

For my example, I’ve added some views on the Main Activity’s layout to enter a Github login, trigger the http call and display the result.

In the kotlin file of my Main Activity, I’ve added the code to make the request to the API endpoint.

To run the app with the mock URL, we can compile the app with the mock Build Variant with the command below in the project root folder.

> ./gradlew app:assembleMockGithub

We can now create our test and assert the UI. Given that the Http call is asynchronous, we have to use the IdlingRessource to make the assertion only once the Http response has come back. So we add the dependancy in the module build.gradle.

androidTestImplementation 'com.jakewharton.espresso:okhttp3-idling-resource:1.0.0'

This is a simple test to check if the name retrieved from the Http response is displayed.

Now if I run my tests with:

> ./gradlew app:connectedMockGithubDebugAndroidTest

It runs with no errors.

This trick may seem obvious but is a fast and easy way for testing an app without loosing too much time on the server emulation. In addition, Firebase is free under a certain quota so it’s largely enough for some daily tests.

You can check out the whole code on my Github page :

Big thanks to florent champigny for helping me to write this article.

--

--

Android developer at @Idean and passionate about many other technologies.