A kotlin tool to intercept android network calls, modify requests/responses and mock entire APIs. Also includes a cool DSL, that helps to reduce boilerplate code and simplify development.
btw, Iris is my daughter's name 🥰
- Works with Retrofit, Volley and other libs that depend on OkHttp
- Allow intercept call on 3rd-party libs
- DSL to avoid boilerplate
- A centralized tool to log, intercept and modify requests/response
- No need to do SSL things, like inject certificate
- Simple and intuitive API
- Doesn't use reflection
- As it works at bytecode level, can be used with 3rd-party libs (plugin-only)
This is the default way of using IrisMock. Start by adding the plugin to your project:
// :app build.gradle.kts
plugins {
id("dev.arildo.iris-mock-plugin") version "LATEST_VERSION"
}Using legacy plugin application - older gradle
// :app build.gradle
buildscript {
repositories {
maven {
url = uri("https://plugins.gradle.org/m2/")
}
}
dependencies {
classpath("dev.arildo:iris-mock-plugin:LATEST_VERSION")
}
}
apply(plugin = "dev.arildo.iris-mock-plugin")Then create a class implementing the Interceptor interface and then use irisMock(chain) function:
class MyInterceptor : Interceptor {
override fun intercept(chain: Interceptor.Chain) = irisMock(chain) {
onGet(contains = "user/profile") mockResponse userProfileJson
onPost(endsWith = "/login") {
delay(2_000) // fake slow internet
if (containsInRequestBody("validPassword")) mockResponse(successLoginJson)
else mockResponse(errorPasswordJson)
}
}
}Finally, we need to start IrisMock on application and pass the interceptors:
class MyApp : Application() {
override fun onCreate() {
super.onCreate()
startIrisMock {
enableLogs()
interceptors(::MyInterceptor)
}
}
}This way, the interceptors are not injected on OkHttp, so you need to it by yourself. It's specially useful if you already have interceptors on your project and want only benefit from using IrisMock DSL.
// build.gradle.kts
dependencies {
implementation("dev.arildo:iris-mock:LATEST_VERSION")
}then you can use irisMock(chain), as shown previously.
See official docs for further details
- Add support to Ktor
- Expand DSL
Feel free to open PRs and submit feature suggestions via the repository issues. Everything's welcome 😎
Copyright 2023 Arildo Borges Junior
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

