com.cocosw:optimus

Optimus is a dynamic mock tool for retrofit


Keywords
android, dynamic-mock, mock, network-behavior, okhttp, retrofit2, testing
License
Apache-2.0

Documentation

Optimus

Maven

Optimus is a dynamic mock tool for retrofit

Sample

Usage

Given you have a service interface

interface Api {
    @POST("api/login")
    fun login(): Call<Void>
}

Step 1 Define mock data

class MockUser : MockResponse {
    @Default
    val HTTP200 = success {}
    val HTTP401 = error(401,"UnAuthorized") { ApiError(401, "Unauthorized") }
}

Step 2 Create an optimus instance

val supplier = MockResponseSupplier.create(sharedpreference)
Optimus.Builder(supplier)
                    .retrofit(retrofit,sharedpreference)
                    .mockGraph(
                               alter(Api::class.java, "Api") {
                                   Api::login with MockUser::class named "Login"
                               })
                    .converter(Converter.create(moshi))
                    .build()

Step 3 Replace retrofit with optimus

optimus.create(Api::class.java)

Optimus provides a view to change mock behavior in runtime, You may use it in a Alert like this

AlertDialog.Builder(this)
           .setView(OptimusView(this).apply { this.setOptimus(optimus) })
           .create().show()

Testing

Optimus makes unittest and UI test easier.

Step1 User InMemory MockResponseSupplier and mockretrofit

val supplier = MockResponseSupplier.memory()

Optimus.Builder(supplier)
       .retrofit(retrofit)
       .mockGraph(mockgraph)
       .build()

Step2 Change mock response with Api

        val api = optimus.create(Api::class.java)
        mockResponseSupplier.set(Api::call, MockUser::HTTP401)
        assert(api.login().response.code,401)

Download

    implementation 'com.cocosw:optimus:1.0.0'