All Projects → javaherisaber → MockFit

javaherisaber / MockFit

Licence: Apache-2.0 License
Kotlin library to mock http responses that fits into retrofit

Programming Languages

kotlin
9241 projects

Projects that are alternatives of or similar to MockFit

Instagram
Instagram Project is a mini version of real Instagram app for Android 📱 built with latest Android Development Tools. Kotlin, MVVM, LiveData, GSON, Retrofit, Dagger2
Stars: ✭ 20 (+42.86%)
Mutual labels:  retrofit
WanAndroid
Kotlin版 玩Android 客户端
Stars: ✭ 37 (+164.29%)
Mutual labels:  retrofit
Retromock
Java library for mocking responses in a Retrofit service.
Stars: ✭ 48 (+242.86%)
Mutual labels:  retrofit
Android-Model-View-Presenter
No description or website provided.
Stars: ✭ 26 (+85.71%)
Mutual labels:  retrofit
WanAndroid-Java
一款采用Java语言、MVVM + Retrofit + RxJava架构开发的玩Android客户端 (https://www.wanandroid.com/) 。PS: Kotlin版 (https://github.com/chongyucaiyan/WanAndroid-Kotlin) 。
Stars: ✭ 32 (+128.57%)
Mutual labels:  retrofit
aLiangWanAndroid
玩Android项目
Stars: ✭ 19 (+35.71%)
Mutual labels:  retrofit
ComposeMovie
Movie app that built with Jetpack Compose
Stars: ✭ 96 (+585.71%)
Mutual labels:  retrofit
EasyRetro
An Easy to use retrofit based network/api call extention for android
Stars: ✭ 16 (+14.29%)
Mutual labels:  retrofit
Movie-Flutter
Movie App Flutter using Flutter BLoC, DIO, Retrofit, Sqflite
Stars: ✭ 36 (+157.14%)
Mutual labels:  retrofit
Dribbbler
A Dribbble client with MVP+Repository pattern , RxJava ,Retrofit , Dagger
Stars: ✭ 56 (+300%)
Mutual labels:  retrofit
EasyTask MVVM Kotlin
Todo app based on MVVM, Kotlin Coroutines, Navigation Component, Room Database, Retrofit, Data Binding
Stars: ✭ 49 (+250%)
Mutual labels:  retrofit
WanAndroidJetpack
🔥 WanAndroid 客户端,Kotlin + MVVM + Jetpack + Retrofit + Glide。基于 MVVM 架构,用 Jetpack 实现,网络采用 Kotlin 的协程和 Retrofit 配合使用!精美的 UI,便捷突出的功能实现,欢迎下载体验!
Stars: ✭ 124 (+785.71%)
Mutual labels:  retrofit
Askme
Social media app to ask and answer user questions and interact with users
Stars: ✭ 16 (+14.29%)
Mutual labels:  retrofit
android
Android client for Mahadel project [Deprecated]
Stars: ✭ 25 (+78.57%)
Mutual labels:  retrofit
WanAndroid
💪 WanAndroid应用,持续更新,不断打造成一款持续稳定, 功能完善的应用
Stars: ✭ 50 (+257.14%)
Mutual labels:  retrofit
qa-automation-base
There are basic projects for automation frameworks based on Kotlin/Java and TypeScript for the backend, frontend, and mobile.
Stars: ✭ 45 (+221.43%)
Mutual labels:  retrofit
uv-index
This is a work-in-progress (🔧️) ultraviolet index viewer app for demonstrating Instant Apps + Kotlin + Dagger + MVP
Stars: ✭ 64 (+357.14%)
Mutual labels:  retrofit
tuya-connector
tuya-connector helps you efficiently create cloud development projects regarding the OpenAPI or message subscription capabilities. You can put all the focus on business logic without taking care of server-side programming nor relational databases.
Stars: ✭ 28 (+100%)
Mutual labels:  retrofit
gv4j
Unofficial Google Voice API for Java.
Stars: ✭ 18 (+28.57%)
Mutual labels:  retrofit
PlayAndroid
✌️✊👋玩安卓Mvvm组件化客户端,整合Jetpack组件DataBinding、ViewModel以及LiveData;屏幕适配✔️状态栏沉浸式✔️黑夜模式✔️,无数据、加载失败状态页;骨架屏、Koin依赖注入等
Stars: ✭ 193 (+1278.57%)
Mutual labels:  retrofit

MockFit

Kotlin library to mock http responses that fits into retrofit from square

Dependency

Top level build.gradle

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

Module level build.gradle

dependencies {
  implementation "com.github.javaherisaber.MockFit:runtime:$versions.mockFit"
  kapt "com.github.javaherisaber.MockFit:compiler:$versions.mockFit" // for Kotlin (make sure to include kapt plugin also)
  annotationProcessor "com.github.javaherisaber.MockFit:compiler:$versions.mockFit" // for Java
}

How to use

  1. Create json file of your api response lets say picsum_list.json and put it in assets/mock_json directory
[
  {
    "id": "1016",
    "author": "Andrew Ridley",
    "width": 3844,
    "height": 2563,
    "url": "https://unsplash.com/photos/_h7aBovKia4",
    "download_url": "https://picsum.photos/id/1018/3914/2935"
  },
  {
    "id": "1018",
    "author": "Andrew Ridley",
    "width": 3914,
    "height": 2935,
    "url": "https://unsplash.com/photos/Kt5hRENuotI",
    "download_url": "https://picsum.photos/id/1018/3914/2935"
  },
  {
    "id": "1019",
    "author": "Patrick Fore",
    "width": 5472,
    "height": 3648,
    "url": "https://unsplash.com/photos/V6s1cmE39XM",
    "download_url": "https://picsum.photos/id/1019/5472/3648"
  }
]
  1. Define your api interface and annotate endpoint with @mock("picsum_list.json") so that Mockfit can generate config for you
interface Api {

    @Mock("picsum_list.json")
    @GET("list")
    fun getListOfPicsums(
        @Query("page") page: Int,
        @Query("limit") limit: Int
    ): Call<List<Picsum>>
}
  1. Add MockfitInterceptor to retrofit configuration
class RemoteDataSource(private val context: Context) {

    fun api(): Api {
        val mockFitInterceptor = provideMockFitInterceptor(context)
        val okHttpClient = provideOkHttpClient(mockFitInterceptor)
        val retrofit = provideRetrofit(okHttpClient)
        return retrofit.create(Api::class.java)
    }

    private fun provideRetrofit(okHttpClient: OkHttpClient): Retrofit = Retrofit.Builder()
        .baseUrl(BASE_URL)
        .addConverterFactory(GsonConverterFactory.create())
        .client(okHttpClient)
        .build()

    private fun provideMockFitInterceptor(context: Context) = MockFitInterceptor(
        bodyFactory = { input -> context.resources.assets.open(input) }, // read asset file
        logger = { tag, message -> Log.d(tag, message) }, // pass logger to log events in logcat
        baseUrl = BASE_URL, // base url of your api
        requestPathToJsonMap = REQUEST_TO_JSON, // autogenerated constant, just press build button
        mockFilesPath = MOCK_FILES_PATH, // path to json files
        mockFitEnable = true, // master setting to enable or disable mocking
        apiEnableMock = true, // enable or disable mock when there are includes and excludes configs
        apiIncludeIntoMock = arrayOf(), // include endpoint if `apiEnableMock` is false
        apiExcludeFromMock = arrayOf(), // exclude endpoint if `apiEnableMock` is true 
        apiResponseLatency = 500L // latency of retrieving data
    )

    private fun provideOkHttpClient(mockFitInterceptor: MockFitInterceptor) = OkHttpClient.Builder()
        .addInterceptor(mockFitInterceptor)
        .connectTimeout(CONNECT_TIMEOUT, TimeUnit.SECONDS)
        .writeTimeout(WRITE_TIMEOUT, TimeUnit.SECONDS)
        .readTimeout(READ_TIMEOUT, TimeUnit.SECONDS)
        .build()

    companion object {
        private const val BASE_URL = "https://picsum.photos/v2/"
        private const val MOCK_FILES_PATH = "mock_json" // located at assets/mock_json/
        private const val CONNECT_TIMEOUT = 20L
        private const val WRITE_TIMEOUT = 20L
        private const val READ_TIMEOUT = 30L
    }
}
  1. Rebuild (or just build current module)
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].