项目作者: soarcn

项目描述 :
Optimus is a dynamic mock tool for retrofit
高级语言: Kotlin
项目地址: git://github.com/soarcn/Optimus.git
创建时间: 2020-08-04T07:06:50Z
项目社区:https://github.com/soarcn/Optimus

开源协议:Apache License 2.0

下载


Optimus

Maven

Optimus is a dynamic mock tool for retrofit

Sample

Usage

Given you have a service interface

  1. interface Api {
  2. @POST("api/login")
  3. fun login(): Call<Void>
  4. }

Step 1 Define mock data

  1. class MockUser : MockResponse {
  2. @Default
  3. val HTTP200 = success {}
  4. val HTTP401 = error(401,"UnAuthorized") { ApiError(401, "Unauthorized") }
  5. }

Step 2 Create an optimus instance

  1. val supplier = MockResponseSupplier.create(sharedpreference)
  2. Optimus.Builder(supplier)
  3. .retrofit(retrofit,sharedpreference)
  4. .mockGraph(
  5. alter(Api::class.java, "Api") {
  6. Api::login with MockUser::class named "Login"
  7. })
  8. .converter(Converter.create(moshi))
  9. .build()

Step 3 Replace retrofit with optimus

  1. optimus.create(Api::class.java)

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

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

Testing

Optimus makes unittest and UI test easier.

Step1 User InMemory MockResponseSupplier and mockretrofit

  1. val supplier = MockResponseSupplier.memory()
  2. Optimus.Builder(supplier)
  3. .retrofit(retrofit)
  4. .mockGraph(mockgraph)
  5. .build()

Step2 Change mock response with Api

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

Download

  1. implementation 'com.cocosw:optimus:1.0.0'