All Projects → mirrajabi → Okhttp Json Mock

mirrajabi / Okhttp Json Mock

Licence: apache-2.0
Mock your datas for Okhttp and Retrofit in json format in just a few moves

Programming Languages

java
68154 projects - #9 most used programming language

Projects that are alternatives of or similar to Okhttp Json Mock

Kakapo.js
🐦 Next generation mocking framework in Javascript
Stars: ✭ 535 (+131.6%)
Mutual labels:  interceptor, mock, mocking
Http Fake Backend
Build a fake backend by providing the content of JSON files or JavaScript objects through configurable routes.
Stars: ✭ 253 (+9.52%)
Mutual labels:  json, mock, mocking
Logginginterceptor
An OkHttp interceptor which has pretty logger for request and response. +Mock support
Stars: ✭ 1,149 (+397.4%)
Mutual labels:  okhttp, interceptor, mock
Retromock
Java library for mocking responses in a Retrofit service.
Stars: ✭ 48 (-79.22%)
Mutual labels:  mock, retrofit, mocking
Retrofitcache
RetrofitCache让retrofit2+okhttp3+rxjava配置缓存如此简单。通过注解配置,可以针对每一个接口灵活配置缓存策略;同时让每一个接口方便支持数据模拟,可以代码减小侵入性,模拟数据可以从内存,Assets,url轻松获取。
Stars: ✭ 647 (+180.09%)
Mutual labels:  okhttp, retrofit, 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 (-78.79%)
Mutual labels:  json, mock, mocking
Livedata Call Adapter
A simple LiveData call adapter for retrofit
Stars: ✭ 119 (-48.48%)
Mutual labels:  json, okhttp, retrofit
Mockiato
A strict, yet friendly mocking library for Rust 2018
Stars: ✭ 229 (-0.87%)
Mutual labels:  mock, mocking
Examples
Examples of Mock Service Worker usage with various frameworks and libraries.
Stars: ✭ 163 (-29.44%)
Mutual labels:  interceptor, mocking
Axios Mock Adapter
Axios adapter that allows to easily mock requests
Stars: ✭ 2,832 (+1125.97%)
Mutual labels:  mock, mocking
Component
🔥🔥🔥A powerful componentized framework.一个强大、100% 兼容、支持 AndroidX、支持 Kotlin并且灵活的组件化框架
Stars: ✭ 2,434 (+953.68%)
Mutual labels:  interceptor, retrofit
Mockito
Most popular Mocking framework for unit tests written in Java
Stars: ✭ 12,453 (+5290.91%)
Mutual labels:  mock, mocking
Httpretty
Intercept HTTP requests at the Python socket level. Fakes the whole socket module
Stars: ✭ 1,930 (+735.5%)
Mutual labels:  mock, mocking
Mocktail
A mock library for Dart inspired by mockito
Stars: ✭ 172 (-25.54%)
Mutual labels:  mock, mocking
Aeromock
Lightweight mock web application server
Stars: ✭ 152 (-34.2%)
Mutual labels:  json, mock
Interceptor
A browser extension to mock AJAX requests at the browser level
Stars: ✭ 182 (-21.21%)
Mutual labels:  interceptor, mock
Retrofiturlmanager
🔮 Let Retrofit support multiple baseUrl and can be change the baseUrl at runtime (以最简洁的 Api 让 Retrofit 同时支持多个 BaseUrl 以及动态改变 BaseUrl).
Stars: ✭ 1,961 (+748.92%)
Mutual labels:  okhttp, retrofit
Mocktopus
Mocking framework for Rust
Stars: ✭ 179 (-22.51%)
Mutual labels:  mock, mocking
Templeapp
Android App which handles the information about temple. People can register and keep a track of all poojas, donations made to the temple.
Stars: ✭ 231 (+0%)
Mutual labels:  json, okhttp
Progressmanager
⏳ Listen the progress of downloading and uploading in Okhttp, compatible Retrofit and Glide (一行代码即可监听 App 中所有网络链接的上传以及下载进度, 包括 Glide 的图片加载进度).
Stars: ✭ 2,463 (+966.23%)
Mutual labels:  okhttp, retrofit

okhttp-json-mock

Android Arsenal

This simple library helps you mock your data for using with okhttp+retrofit in json format in just a few moves. it forwards the requests to local json files and returns the data stored in them.

Version 3.0 Notes:

3.0 introduces breaking changes, since it removes the wrapper for mocked responses (MockedResponse.java) and therefor does not alter the api anymore. Data transfer objects are now accessed directly without embedding them into an additional json object. See the Version 2.0 Documentation for the old api.

Version 2.0 Notes:

Since version 2.0 the dependency to android platform is removed so it will be useful for all your jvm-based projects, not just android. You can still use version 1.1.1 if you don't care.

Usage

First add jitpack to your projects build.gradle file

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

Then add the dependency in modules build.gradle file

dependencies {
    compile 'com.github.mirrajabi:okhttp-json-mock:3.0'
 }

Since version 2.0:

  1. Construct your custom InputStreamProvider:
InputStreamProvider inputStreamProvider = new InputStreamProvider() {
    @Override
    public InputStream provide(String path) {
        try {
            return getAssets().open(path);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
};
  1. Use the InputStreamProvider to construct the OkHttpMockInterceptor and client:
OkHttpClient client = new OkHttpClient.Builder()
    .addInterceptor(new OkHttpMockInterceptor(getAndroidProvider(), 5))
    .build();

For version 1.+

1. Add OkhttpMockInterceptor to your OkhttpClient instance and attach it to your retrofit instance

OkHttpClient mOkHttpClient = new OkHttpClient.Builder()
    .addInterceptor(new OkHttpMockInterceptor(this, 5))
    .build();
    
mRetrofit = new Retrofit.Builder()
    .addConverterFactory(GsonConverterFactory.create())
    .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
    .baseUrl("http://example.com")
    .client(mOkHttpClient)
    .build();

2. Prepare your api service interfaces for retrofit

//usage example /users/page=phoneNumbers.json
@GET(API_VERSION + "/users")
Observable<ArrayList<UserModel>> getUsers(@Query("page") int page);

//usage example /users/page=1&secondParameter=phoneNumbers.json
@GET(API_VERSION + "/users")
Observable<ArrayList<UserModel>> getUsers(@Query("page") int page,
                                          @Query("name") String name);

//usage example /users/1.json
@GET(API_VERSION + "/users/{userId}")
Observable<UserModel> getUser(@Path("userId") int userId);

//usage example /users/1/phoneNumbers.json
@GET(API_VERSION + "/users/{userId}/phoneNumbers")
Observable<ArrayList<String>> getUserNumbers(@Path("userId") int userId);

3. Put your json models in assets folder like the examples

\---api
    \---v1
        \---users
            |   1.json
            |   2.json
            |   3.json
            |   page=1.json
            |
            +---1
            |       phoneNumbers.json
            |
            +---2
            |       phoneNumbers.json
            |
            \---3
                    phoneNumbers.json

Retrofit's annotations

Currently @Query and @Path can be achieved simply with correct folder and file namings (like website routes) for example if you have a request like

@GET("api/v1/posts/{userId}")
Observable<ArrayList<Post>> getUserPosts(@Path("userId"),
                                         @Query("page") int page,
                                         @Query("categoryId") int categoryId);

you can have json models in api/v1/posts/{userId} where {userId} could be an integer like api/v1/posts/3 and in that folder the json files should have names like page=1&categoryId=5.json so multiple queries are achievable by seperating them using Ampersand(&) character

You can take a look at Sample app for a working example

Contributions

Any contributions are welcome. just fork it and submit your changes to your fork and then create a pull request

Changelog

3.0 - Removed wrapper for mocked responses

2.0 - The library no longer depends on android classes

1.1.1 - Fixes file name lowercase issue

1.1 - Adds delay customization option.

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].