All Projects → donfuxx → Mockinizer

donfuxx / Mockinizer

Licence: apache-2.0
An okhttp / retrofit api call mocking library

Programming Languages

kotlin
9241 projects

Projects that are alternatives of or similar to Mockinizer

Mockoon
Mockoon is the easiest and quickest way to run mock APIs locally. No remote deployment, no account required, open source.
Stars: ✭ 3,448 (+1859.09%)
Mutual labels:  rest-api, mock, mock-server
Http Fake Backend
Build a fake backend by providing the content of JSON files or JavaScript objects through configurable routes.
Stars: ✭ 253 (+43.75%)
Mutual labels:  rest-api, restful-api, mock
Generator Http Fake Backend
Yeoman generator for building a fake backend by providing the content of JSON files or JavaScript objects through configurable routes.
Stars: ✭ 49 (-72.16%)
Mutual labels:  rest-api, restful-api, mock
Weld
Full fake REST API generator written with Rust
Stars: ✭ 146 (-17.05%)
Mutual labels:  mock, mock-server
Codeigniter Jwt Sample
CodeIgniter JWT Sample
Stars: ✭ 144 (-18.18%)
Mutual labels:  rest-api, restful-api
Node Express Mongoose Passport Jwt Rest Api Auth
Node, express, mongoose, passport and JWT REST API authentication example
Stars: ✭ 146 (-17.05%)
Mutual labels:  rest-api, restful-api
Bilisoleil Kotlin
An unofficial bilibili client for android --kotlin+rxjava2+mvp+okhttp3+retrofit2+dagger2
Stars: ✭ 139 (-21.02%)
Mutual labels:  okhttp3, retrofit2
Appkernel
API development made easy: a smart Python 3 API framework
Stars: ✭ 152 (-13.64%)
Mutual labels:  rest-api, restful-api
Awesome Http Benchmark
HTTP(S) benchmark tools, testing/debugging, & restAPI (RESTful)
Stars: ✭ 2,236 (+1170.45%)
Mutual labels:  rest-api, restful-api
Retrofiturlmanager
🔮 Let Retrofit support multiple baseUrl and can be change the baseUrl at runtime (以最简洁的 Api 让 Retrofit 同时支持多个 BaseUrl 以及动态改变 BaseUrl).
Stars: ✭ 1,961 (+1014.2%)
Mutual labels:  okhttp3, retrofit2
Mifos Mobile
Repository for the Mifos Mobile Banking App for clients
Stars: ✭ 154 (-12.5%)
Mutual labels:  okhttp3, retrofit2
Python Simple Rest Client
Simple REST client for python 3.6+
Stars: ✭ 143 (-18.75%)
Mutual labels:  rest-api, restful-api
Api Generator
Api Generator是一款可以自动解析Controller类抽取REST接口信息并自动上传YApi的IDEA插件。YApi好伴侣,从此维护文档再也不是事儿了!
Stars: ✭ 139 (-21.02%)
Mutual labels:  rest-api, restful-api
Androidbaseframemvvm
Android 组件化 MVVM 框架 基于 Jetpack + Kotlin
Stars: ✭ 169 (-3.98%)
Mutual labels:  okhttp3, retrofit2
Androidstarter
A sample Android app using the MVP architecture.
Stars: ✭ 140 (-20.45%)
Mutual labels:  okhttp3, retrofit2
Swagger meqa
Auto generate and run tests using swagger/OpenAPI spec, no coding needed
Stars: ✭ 151 (-14.2%)
Mutual labels:  rest-api, restful-api
Hikaku
A library that tests if the implementation of a REST-API meets its specification.
Stars: ✭ 154 (-12.5%)
Mutual labels:  rest-api, restful-api
Restful Api With Laravel Definitive Guide
Repository with the base code for the course "RESTful API with Laravel - Definitive-Guide"
Stars: ✭ 156 (-11.36%)
Mutual labels:  rest-api, restful-api
Jbusdriver
这是去幼儿园的班车(滑稽
Stars: ✭ 2,056 (+1068.18%)
Mutual labels:  okhttp3, retrofit2
Open Rest
Standard rest server, Base on restify and sequelize
Stars: ✭ 136 (-22.73%)
Mutual labels:  rest-api, restful-api

Mockinizer Build Status

An OkHttpClient / RetroFit web api call mocking library that uses MockWebServer to provide mocked responses by using Interceptors.

What is it?

Mockinizer helps Android developers to build apps with web api calls that use OkHttpClient / RetroFit by allowing them to quickly mock some web api responses with MockWebServer. Mockinizer allows to mock only specific api calls, while other api calls will still call the original server. This is particularily usefull in the following scenarios:

  • You are working on a new feature that needs to call new not yet existing apis. With Mockinizer you can quickly swap in the new desired mocked api responses while other api calls will still use the real server
  • You want to test error cases for existing apis. With Mockinizer you can can quickly mock an error 500 response or an 401 Unauthorized for selected api requests and verify if your app handles them gracefully
  • You want to call a mocked api for unit testing and isolate those from the webserver

Setup

1. Add jitpack.io repository:

Add jitpack.io repository in root build.gradle:

allprojects {
	repositories {
		...
		maven { url 'https://jitpack.io' }
	}
}

2. Add Mockinizer gradle dependency

Add the below code in the app module's build.gradle (Usually you want to implement it only in debug builds and not release builds) At the time of writing the latest mockinizer_version was 1.1.0, you can get latest release version here: https://github.com/donfuxx/Mockinizer/releases

dependencies {
    debugImplementation "com.github.donfuxx:Mockinizer:1.6.0"
}

You may also need to add a MockWebServer dependency in your app module:

dependencies {
    implementation "com.squareup.okhttp3:mockwebserver:4.0.1"
}

3. Define the RequestFilter / MockResponse Pairs

Those represent each api call that you want to mock. The RequestFilter defines the Request Path (relative to the RetroFit Baseurl) and/or the json body of the request. The MockResponse is the desired Response that you want to get returned by your local MockWebServer. See a simple example that defines 2 mock responses where one out of them is an error:

package com.appham.mockinizer.demo

import com.appham.mockinizer.RequestFilter
import okhttp3.mockwebserver.MockResponse

val mocks: Map<RequestFilter, MockResponse> = mapOf(

    RequestFilter("/mocked") to MockResponse().apply {
        setResponseCode(200)
        setBody("""{"title": "Banana Mock"}""")
    },

    RequestFilter("/mockedError") to MockResponse().apply {
        setResponseCode(400)
    }

)

4. Add mockinizer to OkHttpClient

To wire up the mocks that you defined in step 3 you just have to call the mockinize(mocks) extension function in the OkHttpClient builder and provide the mocks map as the parameter, see example:

OkHttpClient.Builder()
            .addInterceptor(loggingInterceptor)
            .mockinize(mocks) // <-- just add this line
            .build()

Yes, that is it! Just add .mockinize(mocks) into the OkHttpClient.Builder chain. Happy mocking! :-)

5. Launch app and check logs to verify mocking is working

Once you call a mockinized api endpoint in your app then you can verify the mocked responses in the logcat. The attached HttpLogging interceptor should produce logs similar to:

D/OkHttp: --> GET https://my-json-server.typicode.com/typicode/demo/mockedError
D/OkHttp: --> END GET
D/OkHttp: --> GET https://my-json-server.typicode.com/typicode/demo/mocked
D/OkHttp: --> END GET
D/OkHttp: --> GET https://my-json-server.typicode.com/typicode/demo/posts
D/OkHttp: --> END GET
D/OkHttp: <-- 400 https://localhost:34567/typicode/demo/mockedError (97ms)
D/OkHttp: content-length: 0
D/OkHttp: mockinizer: <-- Real request /typicode/demo/mockedError is now mocked to HTTP/1.1 400 Client Error
D/OkHttp: server: Mockinizer 1.6.0 by Thomas Fuchs-Martin
D/OkHttp: <-- END HTTP (0-byte body)
D/OkHttp: <-- 200 https://localhost:34567/typicode/demo/mocked (104ms)
D/OkHttp: content-length: 98
D/OkHttp: mockinizer: <-- Real request /typicode/demo/mocked is now mocked to HTTP/1.1 200 OK
D/OkHttp: server: Mockinizer 1.6.0 by Thomas Fuchs-Martin
D/OkHttp: [
D/OkHttp:   {
D/OkHttp:     "id": 555,
D/OkHttp:     "title": "Banana Mock"
D/OkHttp:   },
D/OkHttp:   {
D/OkHttp:     "id": 675,
D/OkHttp:     "title": "foooo"
D/OkHttp:   }
D/OkHttp: ]
D/OkHttp: <-- END HTTP (98-byte body)
D/OkHttp: <-- 200 https://my-json-server.typicode.com/typicode/demo/posts (1416ms)
D/OkHttp: date: Sat, 28 Mar 2020 19:11:32 GMT
D/OkHttp: content-type: application/json; charset=utf-8
D/OkHttp: set-cookie: __cfduid=de53d4c305959e69c3e8ea1e6b78959001585422691; expires=Mon, 27-Apr-20 19:11:31 GMT; path=/; domain=.typicode.com; HttpOnly; SameSite=Lax
D/OkHttp: x-powered-by: Express
D/OkHttp: vary: Origin, Accept-Encoding
D/OkHttp: access-control-allow-credentials: true
D/OkHttp: cache-control: no-cache
D/OkHttp: pragma: no-cache
D/OkHttp: expires: -1
D/OkHttp: x-content-type-options: nosniff
D/OkHttp: etag: W/"86-YtXc+x6dfp/4aT8kTDdp4oV+9kU"
D/OkHttp: via: 1.1 vegur
D/OkHttp: cf-cache-status: DYNAMIC
D/OkHttp: expect-ct: max-age=604800, report-uri="https://report-uri.cloudflare.com/cdn-cgi/beacon/expect-ct"
D/OkHttp: server: cloudflare
D/OkHttp: cf-ray: 57b3a8502b8a71f7-AMS
D/OkHttp: [
D/OkHttp:   {
D/OkHttp:     "id": 1,
D/OkHttp:     "title": "Post 1"
D/OkHttp:   },
D/OkHttp:   {
D/OkHttp:     "id": 2,
D/OkHttp:     "title": "Post 2"
D/OkHttp:   },
D/OkHttp:   {
D/OkHttp:     "id": 3,
D/OkHttp:     "title": "Post 3"
D/OkHttp:   }
D/OkHttp: ]
D/OkHttp: <-- END HTTP (134-byte body)

In above example three api calls have been made. The only api call that wasn´t mocked is the call to https://my-json-server.typicode.com/typicode/demo/posts the other calls to /mocked and /mockedError got swapped in by Mockinizer! (Notice localhost responding to those with the previously defined mock responses)

See Mockinizer Demo Project

In case you want to see how it works in action then just check out the Mockinizer demo project!

Run automated tests

Another good way to see Mockinizer in action is to run the androidTests: just run ./gradlew build connectedCheck from the terminal and see in the logcat how api calls got mocked!

Contributing

Pull requests are welcome! Just fork Mockinizer and add submit your PR. Also feel free to add issues for new feature requests and discovered bugs. Check the Contributing guidelines for more infos.

Feedback

If you like this project then please don't forget to stargaze Mockinizer and share it with your friends!

Stay up to date

Always be the first to know about new releases and add Mockinizer to your watches.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].