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
- Host: GitHub
- URL: https://github.com/soarcn/optimus
- Owner: soarcn
- License: apache-2.0
- Created: 2020-08-04T07:06:50.000Z (almost 6 years ago)
- Default Branch: master
- Last Pushed: 2020-08-05T07:14:41.000Z (almost 6 years ago)
- Last Synced: 2025-03-05T16:37:05.576Z (over 1 year ago)
- Topics: android, dynamic-mock, mock, network-behavior, okhttp, retrofit2, testing
- Language: Kotlin
- Homepage:
- Size: 1.29 MB
- Stars: 4
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
Optimus
============

Optimus is a dynamic mock tool for retrofit

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