项目作者: mirrajabi

项目描述 :
Mock your datas for Okhttp and Retrofit in json format in just a few moves
高级语言: Java
项目地址: git://github.com/mirrajabi/okhttp-json-mock.git
创建时间: 2017-02-08T17:32:40Z
项目社区:https://github.com/mirrajabi/okhttp-json-mock

开源协议:Apache License 2.0

下载


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

  1. allprojects {
  2. repositories {
  3. ...
  4. maven { url "https://jitpack.io" }
  5. }
  6. }

Then add the dependency in modules build.gradle file

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

Since version 2.0:

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

For version 1.+

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

  1. OkHttpClient mOkHttpClient = new OkHttpClient.Builder()
  2. .addInterceptor(new OkHttpMockInterceptor(this, 5))
  3. .build();
  4. mRetrofit = new Retrofit.Builder()
  5. .addConverterFactory(GsonConverterFactory.create())
  6. .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
  7. .baseUrl("http://example.com")
  8. .client(mOkHttpClient)
  9. .build();

2. Prepare your api service interfaces for retrofit

  1. //usage example /users/page=phoneNumbers.json
  2. @GET(API_VERSION + "/users")
  3. Observable<ArrayList<UserModel>> getUsers(@Query("page") int page);
  4. //usage example /users/page=1&secondParameter=phoneNumbers.json
  5. @GET(API_VERSION + "/users")
  6. Observable<ArrayList<UserModel>> getUsers(@Query("page") int page,
  7. @Query("name") String name);
  8. //usage example /users/1.json
  9. @GET(API_VERSION + "/users/{userId}")
  10. Observable<UserModel> getUser(@Path("userId") int userId);
  11. //usage example /users/1/phoneNumbers.json
  12. @GET(API_VERSION + "/users/{userId}/phoneNumbers")
  13. Observable<ArrayList<String>> getUserNumbers(@Path("userId") int userId);

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

  1. \---api
  2. \---v1
  3. \---users
  4. | 1.json
  5. | 2.json
  6. | 3.json
  7. | page=1.json
  8. |
  9. +---1
  10. | phoneNumbers.json
  11. |
  12. +---2
  13. | phoneNumbers.json
  14. |
  15. \---3
  16. 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

  1. @GET("api/v1/posts/{userId}")
  2. Observable<ArrayList<Post>> getUserPosts(@Path("userId"),
  3. @Query("page") int page,
  4. @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.