Skip to main content

Mockforme Android SDK

Seamless API Mocking for Android Applications

Introduction

Mockforme allows you to intercept and mock apis in your Android app without changing your API endpoints or code logic. Just enable mock APIs in the Mockforme Dashboard and they will be applied instantly.

Quick Mock APIs

Just initialize Mockforme and add the interceptor. That's it! Initialize. Intercept. Done.

It integrates directly with OkHttp, making it compatible with Retrofit and other networking libraries.

Installation

1. Add Dependencies

Add the following to your module-level build.gradle.kts file:

Important

You must provide your own OkHttp dependency. Mockforme is compatible with both OkHttp 3.x and 4.x.

build.gradle.kts
dependencies {
// Add Mockforme SDK
implementation("com.mockforme:mockforme-android:1.0.7")

// Add OkHttp (Required)
implementation("com.squareup.okhttp3:okhttp:4.12.0")
}

Integration

2. Initialize the SDK

Initialize Mockforme with your access token. This should be done as early as possible, for example in your Application class or main Activity.

MainActivity.kt
Mockforme("YOUR_ACCESS_TOKEN").run(
successCallback = { mappings ->
Log.d("Mockforme", "Successfully loaded ${mappings.size} mappings")
},
errorCallback = { error ->
Log.e("Mockforme", "Failed to initialize", error)
}
)

3. Add the Interceptor

Add MockformeInterceptor to your OkHttpClient builder.

val client = OkHttpClient.Builder()
.addInterceptor(MockformeInterceptor())
.build()

4. Retrofit Integration (Optional)

If you are using Retrofit, simply pass the configured OkHttpClient to your Retrofit builder.

// 1. Create OkHttpClient with Mockforme
val okHttpClient = OkHttpClient.Builder()
.addInterceptor(MockformeInterceptor())
.build()

// 2. Use it in Retrofit
val retrofit = Retrofit.Builder()
.baseUrl("https://api.example.com/")
.client(okHttpClient)
.addConverterFactory(GsonConverterFactory.create())
.build()

Community

Connect with us and learn more about mock apis:

Troubleshooting & FAQ

Does it support OkHttp 3?

Yes! Starting from version 1.0.7, Mockforme fully supports both OkHttp 3.x and 4.x. If you see a NoSuchFieldError, please ensure you are using the latest version of the library.

My requests are not being mocked.

  • Ensure Mockforme("ACCESS_TOKEN").run() is called and the successCallback is triggered.
  • Check if your request URL matches the rules defined in your Mockforme dashboard.
  • Verify that you have added the MockformeInterceptor to your OkHttpClient.

I get an "IllegalStateException: Not initialized".

This happens if the interceptor intercepts a request before Mockforme.run() has been called. Ensure you initialize the SDK before making network calls.