Skip to content

mustafayigitt/MockResponseInterceptor

Repository files navigation

MockResponseInterceptor

MockResponseInterceptor - Request from Local Jsons without any changes. Dynamic configuration change support!

mustafayigitt - mockresponseinterceptor

stars - mockresponseinterceptor forks - mockresponseinterceptor issues - mockresponseinterceptor

GitHub release License

image

With MockResponseInterceptor, fetch data from local json files by retrofit endpoints without changes.

Features

  • Easy use with annotation
  • Test your app locally
  • Change global mocking dynamically
  • Customize filename extractor
  • Custom filename
  • Custom Status code

Dependencies

Add the Jitpack source to project:

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

Add the following dependency to your build.gradle(module) file:

  dependencies {
    ...
    implementation 'com.github.mustafayigitt:MockResponseInterceptor:1.0.0'
  }

Init

MockResponseInterceptor.Builder(context.assets).build()

// or

MockResponseInterceptor.Builder(context.assets)
    .isGlobalMockingEnabled { MainActivity.isGlobalMockingEnabled }
    .fileNameExtractor { url -> "yourNamingStrategy" }
    .build()

Usage

Define your endpoint

// Default - no file name, statusCode = 200 - > top-headlines_get_200.json
@GET("top-headlines")
@Mock
suspend fun getNews(
    @Query("language") country: String = "en",
    @Query("apiKey") apiKey: String = BuildConfig.NEWS_API_KEY,
): Response<NewsWrapperModel>


// With response Code -> top-headlines_get_404.json
@GET("top-headlines")
@Mock(responseCode = 404)
suspend fun getNewsError404(
    @Query("language") country: String = "en",
    @Query("apiKey") apiKey: String = BuildConfig.NEWS_API_KEY,
): Response<NewsWrapperModel>


// With response Code -> top-headlines_get_403.json
@GET("top-headlines")
@Mock(responseCode = 403)
suspend fun getNewsError403(
    @Query("language") country: String = "en",
    @Query("apiKey") apiKey: String = BuildConfig.NEWS_API_KEY,
): Response<NewsWrapperModel>


// With filename -> test_file.json
@GET("top-headlines")
@Mock(fileName = "test_file.json")
suspend fun getNewsCustomFileName(
    @Query("language") country: String = "en",
    @Query("apiKey") apiKey: String = BuildConfig.NEWS_API_KEY,
): Response<NewsWrapperModel>


// With filename -> test_file.json
@GET("top-headlines")
@Mock(fileName = "test_file.json", responseCode = 400)
suspend fun getNewsCustomFileNameResponseCode(
    @Query("language") country: String = "en",
    @Query("apiKey") apiKey: String = BuildConfig.NEWS_API_KEY,
): Response<NewsWrapperModel>

Create Mock Json File

{
  "articles": [
    {
      "title": "MockResponseInterceptor - Mock your Retrofit API responses",
      "urlToImage": "https://picsum.photos/200"
     }
  ]
}

Contribution

We welcome all contributions to mockresponseinterceptor! Please open issue when detect a problem. Every issue will be open to review and merge with pull requests.

License

Copyright 2023 @mustafayigitt

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

About

MockResponseInterceptor - Request from Local Jsons without any changes. Dynamic configuration change support!

Topics

Resources

Stars

Watchers

Forks

Packages

No packages published

Contributors 2

  •  
  •  

Languages