An open API service indexing awesome lists of open source software.

https://github.com/soarcn/optimus

Optimus is a dynamic mock tool for retrofit
https://github.com/soarcn/optimus

android dynamic-mock mock network-behavior okhttp retrofit2 testing

Last synced: 4 months ago
JSON representation

Optimus is a dynamic mock tool for retrofit

Awesome Lists containing this project

README

          

Optimus
============
![Maven](https://maven-badges.herokuapp.com/maven-central/com.cocosw/optimus/badge.png?style=plastic)

Optimus is a dynamic mock tool for retrofit

![Sample](https://github.com/soarcn/Optimus/blob/master/optimus.gif?raw=true)

Usage
=======

Given you have a service interface

```kotlin
interface Api {
@POST("api/login")
fun login(): Call
}
```

Step 1 Define mock data

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

Step 2 Create an optimus instance

```kotlin
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

```kotlin
optimus.create(Api::class.java)
```

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

```kotlin
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

```kotlin
val supplier = MockResponseSupplier.memory()

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

Step2 Change mock response with Api

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

Download
=====
```groovy
implementation 'com.cocosw:optimus:1.0.0'
```