Android: Simple network mocking with Flipper

Tadeáš Sosín
MateeDevs
Published in
3 min readApr 14, 2020

Probably all of you heard about Flipper — debugging platform for mobile developers developed by Facebook. For those who don’t know Flipper let’s read https://fbflipper.com first. Following part shows you only new functionality (v35.0.0+) which allows to mock network request.

Since Flipper publication, you can add the following setup and watch network traffic.

gradle dependencies
Gradle dependencies
Flipper initialization + adding OkHttp interceptor

Now you can inspect your network traffic in a Flipper application.

Network traffic inspection

For quite a long time there was a feature request for Network Mocking. It was finally merged into the master on the 23rd of March! Let’s take a look at how we can easily mock any request using Flipper.

Network mocking setup

First, update the app dependency & Flipper standalone app to version 35.0.0 and above.

Mocking is not activated by default, you have to enable it during the Flipper setup. Let’s change your FlipperOkhttpInterceptor initialization with the following code.

FlipperOkhttpIntercetor initialization

Warning — I had to initialize flipperOkHttpInterceptor right after networkFlipperPlugin. When my OkHttpClient with flipperOkHttpInterceptor creation was done later (after Flipper start) then I didn’t see Mock button in Flipper.

After you run your app with mentioned changes you should see the Mock button in Flipper.

After proper setup, you’ll see the mock button

Now you can mock any response using Flipper. You only need to specify your mocked URL and its response. Right now Flipper only supports GET/POST requests.

Network mock specification

After you close Mock Network Responses dialog your mocked responses will start to work. A mocked request has orange color in Flipper.

And that’s it. Flipper network mocking is really simple and has only basic functionality but you can make it work only in a few minutes.

Demo

Thanks for reading! And happy coding!

--

--