MockResponseInterceptor - Request from Local Jsons without any changes. Dynamic configuration change support!
With MockResponseInterceptor, fetch data from local json files by retrofit endpoints without changes.
- Easy use with annotation
- Test your app locally
- Change global mocking dynamically
- Customize filename extractor
- Custom filename
- Custom Status code
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'
}
MockResponseInterceptor.Builder(context.assets).build()
// or
MockResponseInterceptor.Builder(context.assets)
.isGlobalMockingEnabled { MainActivity.isGlobalMockingEnabled }
.fileNameExtractor { url -> "yourNamingStrategy" }
.build()
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"
}
]
}
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.
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.