{"id":19188804,"url":"https://github.com/miquido/rest-mock","last_synced_at":"2025-08-20T14:08:18.350Z","repository":{"id":54850587,"uuid":"330661583","full_name":"miquido/rest-mock","owner":"miquido","description":"The project was made by Miquido. https://www.miquido.com/","archived":false,"fork":false,"pushed_at":"2021-01-26T11:49:37.000Z","size":147,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":3,"default_branch":"main","last_synced_at":"2025-02-23T03:42:21.990Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"Kotlin","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/miquido.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2021-01-18T12:37:49.000Z","updated_at":"2024-12-16T07:33:38.000Z","dependencies_parsed_at":"2022-08-14T04:40:54.978Z","dependency_job_id":null,"html_url":"https://github.com/miquido/rest-mock","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/miquido/rest-mock","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/miquido%2Frest-mock","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/miquido%2Frest-mock/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/miquido%2Frest-mock/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/miquido%2Frest-mock/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/miquido","download_url":"https://codeload.github.com/miquido/rest-mock/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/miquido%2Frest-mock/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":271330292,"owners_count":24740815,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-08-20T02:00:09.606Z","response_time":69,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-09T11:26:05.355Z","updated_at":"2025-08-20T14:08:18.311Z","avatar_url":"https://github.com/miquido.png","language":"Kotlin","funding_links":[],"categories":[],"sub_categories":[],"readme":"# RestMock\n\nLibrary that provides mocked responses for REST API calls by using [OkHttp MockWebServer](https://github.com/square/okhttp/tree/master/mockwebserver).\n\nRestMock library can be useful, for example, for Android developers to build apps that use OkHttpClient allowing them to mock some API responses. It can be helpful when you work with endpoints that are not available yet, so you can mock them, while other API calls will call the original server. Or you want to test different scenarios with responses or error responses. Or you can call mocked API in unit tests.\n\n\n## I. Setup.\n\n```\nrepositories {\n    jcenter()\n}\n```\nAdd RestMock dependency:\n`implementation 'com.miquido.restmock:restmock:1.0.0'`\n\nThe best way to separate app build with mocked api is to create a flavor for it. Let's name it \"devMock\":\n\n```\nflavorDimensions \"environment\"\n\nproductFlavors {\n    dev {\n        applicationIdSuffix \".dev\"\n        versionName versionName + \"-DEV\"\n        manifestPlaceholders = [appName: \"RestMockSample-DEV\"]\n    }\n\n    devMock {\n        applicationIdSuffix \".dev.mock\"\n        versionName versionName + \"-DEV-MOCK\"\n        manifestPlaceholders = [appName: \"RestMockSample-DEV-MOCK\"]\n    }\n\n    prod {\n        manifestPlaceholders = [appName: \"RestMockSample\"]\n    }\n}\n```\n\nDon't forget to exclude mock json files from all builds except a build with mocked API.\nYou can do it simply by adding mock json files to a particular flavor (devMock in our case) source folder or just exclude files this way:\n```\n// exclude mocks for all flavors except devMock\nif (!getGradle().getStartParameter().getTaskNames().toString().contains(\"DevMock\")){\n    packagingOptions {\n        exclude '/mocks/**'\n    }\n}\n```\n\n## II. Mock API responses.\n\nIn general it's up to you where to keep jsons (API mock responses), but you can go the same way as in our sample application.\n\nYou would need to define a folder name for your mocks\n\n```\nprivate const val MOCKS_FOLDER = \"mocks\"\n```\n\nAdd 2 simple methods (you can modify it for your own needs):\n```\nprivate fun getStringFromResource(fileName: String): String {\n    val classLoader = RequestFilter::class.java.classLoader\n    return classLoader?.getResourceAsStream(\"$MOCKS_FOLDER/$fileName\")?.bufferedReader()\n        ?.use { it.readText() }\n        ?: throw IllegalArgumentException(\"Cannot find `$fileName` in resources\")\n}\n\nprivate fun createMockResponse(code: Int, fileName: String?): MockResponse {\n    return MockResponse().apply {\n        setResponseCode(code)\n        setHeader(\"Content-Type\", \"application/json\")\n        if (!fileName.isNullOrBlank()) {\n            setBody(getStringFromResource(fileName))\n        }\n    }\n}\n```\n\nAdd your json mocks under `src/main/resources/mocks` folder. Please note, it must be exactly *resources*, not *res*.\n\nThen create a map of request filters to responses ([RequestFilter, MockResponse] pairs):\n\n```\nval sampleApiMocks: Map\u003cRequestFilter, MockResponse\u003e = mapOf(\n\n    RequestFilter(\n        method = Method.GET,\n        path = Path.Exact(\"/api/v1/cars\")\n    ) to createMockResponse(200, \"get_cars_200.json\"),\n\n    RequestFilter(\n        method = Method.GET,\n        path = Path.Exact(\"/api/v1/cars\"),\n        query = mapOf(\"make\" to Query.Exact(\"Ford\"))\n    ) to createMockResponse(200, \"get_cars_make_ford_200.json\"),\n\n    ...\n    ...\n)\n```\n\nAnd now when you build an OkHttpClient instance set your mocks:\n\n```\nprivate var apiMocked: Boolean = true/false\n...\n...\nOkHttpClient.Builder()\n    .setMocks(apiMocked) {\n        arrayOf(\n            sampleApiMocks\n            // add other api mocks here\n        )\n    }\n    .build()\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmiquido%2Frest-mock","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmiquido%2Frest-mock","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmiquido%2Frest-mock/lists"}