Skip to content

nattb8/ddmock-android

 
 

Repository files navigation

DD Mock

An API mocking library for Android.

Pre-requisite

App must be using OkHttp for networking

Getting started

  1. In your project build.gradle file:
allprojects {
    repositories {
        maven { url 'https://jitpack.io' }
    }
}
  1. In your build.gradle file:

If the project is using AndroidX:

debugImplementation 'com.github.nattb8.ddmock-android:ddmock:[version]'
releaseImplementation 'com.github.nattb8.ddmock-android:ddmock-no-op:[version]'
  1. In your Application class:
class ExampleApplication : Application() {

  override fun onCreate() {
    super.onCreate()
    DDMock.install(this)
    // Other app init code...
  }
}
  1. Add MockInterceptor to OkHttpClient
val clientBuilder = OkHttpClient().newBuilder()
  .addInterceptor(MockInterceptor())
  // Other configurations...
  .build()

Mock API files

  • All API mock files are stored under /assets/mockfiles and are mapped based on the endpoint path and HTTP method.
    • e.g. login mock response file for endpoint POST BASE_URL/mobile-api/v1/auth/login should be stored under mobile-api/v1/auth/login/post
  • For dynamic endpoint url, create directories with { and } for every replacement blocks and parameters
    • e.g. mock files for GET BASE_URL/mobile-api/v1/users/{usersId} should be stored under mobile-api/v1/users/{usersId}/get
    • see sample
  • All mock files need to be JSON files
  • There can be more than one mock file stored under each endpoint path
    • By default, the first file listed (alphabetically ordered) under each endpoint path is selected as the mock response

FAQs

  • Can we only mock some endpoints, while the rest still calls the actual API?
    • Yes. If there are no mock files to return as a response, the app will call the actual API configured in RetrofitBuilder

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Kotlin 100.0%