An API mocking library for Android.
App must be using OkHttp for networking
- In your project
build.gradle
file:
allprojects {
repositories {
maven { url 'https://jitpack.io' }
}
}
- In your
build.gradle
file:
If the project is using AndroidX:
debugImplementation 'com.github.DeloitteDigitalAPAC.ddmock-android:ddmock:[version]'
releaseImplementation 'com.github.DeloitteDigitalAPAC.ddmock-android:ddmock-no-op:[version]'
- In your
Application
class:
class ExampleApplication : Application() {
override fun onCreate() {
super.onCreate()
DDMock.install(this)
// Other app init code...
}
}
- Add
MockInterceptor
to OkHttpClient
val clientBuilder = OkHttpClient().newBuilder()
.addInterceptor(MockInterceptor())
// Other configurations...
.build()
- 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
- 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