All Projects → GabrielSamojlo → Offit

GabrielSamojlo / Offit

Licence: apache-2.0
Simple but powerful API mocking library. Make mocks great again.

Programming Languages

java
68154 projects - #9 most used programming language

Projects that are alternatives of or similar to Offit

Http Fake Backend
Build a fake backend by providing the content of JSON files or JavaScript objects through configurable routes.
Stars: ✭ 253 (+912%)
Mutual labels:  api, mock
Materialhome
一个基于 Material Design 风格设计的图书展示类App,豆瓣图书,在线电子书。
Stars: ✭ 331 (+1224%)
Mutual labels:  api, retrofit
Retromock
Java library for mocking responses in a Retrofit service.
Stars: ✭ 48 (+92%)
Mutual labels:  mock, retrofit
Graphql Faker
🎲 Mock or extend your GraphQL API with faked data. No coding required.
Stars: ✭ 2,361 (+9344%)
Mutual labels:  api, mock
Rxretrojsoup
A simple API-like from html website (scrapper) for Android, RxJava2 ready !
Stars: ✭ 492 (+1868%)
Mutual labels:  api, retrofit
Component
🔥🔥🔥A powerful componentized framework.一个强大、100% 兼容、支持 AndroidX、支持 Kotlin并且灵活的组件化框架
Stars: ✭ 2,434 (+9636%)
Mutual labels:  api, retrofit
Avenging
MVP pattern example on Android: no Dagger or RxJava example
Stars: ✭ 279 (+1016%)
Mutual labels:  api, retrofit
Mockit
A tool to quickly mock out end points, setup delays and more...
Stars: ✭ 1,534 (+6036%)
Mutual labels:  api, mock
Smocker
Smocker is a simple and efficient HTTP mock server and proxy.
Stars: ✭ 465 (+1760%)
Mutual labels:  api, mock
Service Pattern Go
Simple clean Go REST API architecture with dependency injection and mocking example, following SOLID principles.
Stars: ✭ 449 (+1696%)
Mutual labels:  api, mock
Raml Server
run a mocked server JUST based on a RAML API's definition .. zero coding
Stars: ✭ 158 (+532%)
Mutual labels:  api, mock
Just Api
💥 Test REST, GraphQL APIs
Stars: ✭ 768 (+2972%)
Mutual labels:  api, webservice
Smoke
💨 Simple yet powerful file-based mock server with recording abilities
Stars: ✭ 142 (+468%)
Mutual labels:  api, mock
Mockoon
Mockoon is the easiest and quickest way to run mock APIs locally. No remote deployment, no account required, open source.
Stars: ✭ 3,448 (+13692%)
Mutual labels:  api, mock
Duckrails
Development tool to mock API endpoints quickly and easily (docker image available)
Stars: ✭ 1,690 (+6660%)
Mutual labels:  api, mock
EasyRetro
An Easy to use retrofit based network/api call extention for android
Stars: ✭ 16 (-36%)
Mutual labels:  webservice, retrofit
Mygene.info
MyGene.info: A BioThings API for gene annotations
Stars: ✭ 79 (+216%)
Mutual labels:  api, webservice
Rust rocket api authentication
An example of API written in Rust with the rocket.rs framework, with a JWT Authentication
Stars: ✭ 82 (+228%)
Mutual labels:  api, webservice
Sandwich
🥪 A lightweight and standardized Android network response interface for handling successful data and error responses.
Stars: ✭ 370 (+1380%)
Mutual labels:  api, retrofit
Retrofitcache
RetrofitCache让retrofit2+okhttp3+rxjava配置缓存如此简单。通过注解配置,可以针对每一个接口灵活配置缓存策略;同时让每一个接口方便支持数据模拟,可以代码减小侵入性,模拟数据可以从内存,Assets,url轻松获取。
Stars: ✭ 647 (+2488%)
Mutual labels:  retrofit, mock

OffIt

Android Arsenal Download

What is this?

OffIt is simple but powerful mocking library based on Retrofit.

Do you want to build a demo of your app without worries about your backend reliability? Or maybe you want to prototype your app quickly or just don't have any backend yet? And what if you just want to quickly mock your Retrofit responses in your tests?

OffIt is for you! Make mocks great again.

How to download?

Simple add OffIt as a dependency in your app level build.gradle file.

dependencies {
    compile 'com.gabrielsamojlo.offit:offit:1.0.0'
}

You are ready to go!

Configuration

First of all, you need to pass your Retrofit instance to Offit. Its really simple:

 OffIt.withContext(context).withRetrofit(retrofit).build(isMocked).create(ApiService.class);

where retrofit is your good, old Retrofit you are using every day:

Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("http://jsonplaceholder.typicode.com/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();

Mocking

OffIt gives you three ways of mocking your responses:

  • The simple one is just annotating your API Interface without modifying rest of your codebase.

  • The second, more powerful, is using modified classes. In this scenario, changes in your code are necessary.

  • The third one is additional method based on casting your original Call to MockableCall

First way: Basic Mocking

Basic mocking is really simple. You need to use @Mockable annotation to tell OffIt how you want to mock your call :

@GET("/posts/{id}")
@Mockable(responseCode = 200, jsonPath = "get_post.json", responseTime = 1000)
Call<Post> getPost(@Path("id") int postId);

That's it. After enabling OffIt, your call will be mocked with data passed to the annotation. And it will behave like the real one!

Second way: Advanced Mocking with custom Call class

Advanced, more powerful, way to mock things up is by using modified Call and CallBack classes. Together with @Mockables annotation, it gives you the ability to use tags so you can mock many case scenarios.

@GET("/posts/{id}")
@Mockables({
    @Mockable(tag = "success", responseCode = 200, jsonPath = "get_post.json", responseTime = 3500),
    @Mockable(tag = "no_post", responseCode = 422, jsonPath = "get_post_error.json", responseTime = 4500)})
com.gabrielsamojlo.offit.Call<Post> getPost(@Path("id") int postId);

After that, in your Java code you can execute one of your tagged mockable:

com.gabrielsamojlo.offit.Call<List<Post>> call = mApiService.getPost().withTag("no_post");
call.enqueue(new com.gabrielsamojlo.offit.Callback<List<Post>>() {
    @Override
    public void onResponse(com.gabrielsamojlo.offit.Call<List<Post>> call, Response<List<Post>> response) {
        // Your code
    }

    @Override
    public void onFailure(com.gabrielsamojlo.offit.Call<List<Post>> call, Throwable t) {
        // Your code
    }
});

In addition, using this method you can easily configure parameters of your mocked call from Java code :

com.gabrielsamojlo.offit.Call<List<Post>> call = mApiService.getPosts()
                .withResponseTime(100)
                .withResponseCode(404);

Of course, you can get rid of those com.gabrielsamojlo.offit prefixes by removing imports to original Retrofit classes.

Third way: Advanced Mocking with casting

This approach is a combination of two previous ones. You will have to annotate your API Service but this time, you can leave original retrofit Call as a return type:

@GET("/posts/{id}")
@Mockables({
    @Mockable(tag = "success", responseCode = 200, jsonPath = "get_post.json", responseTime = 3500),
    @Mockable(tag = "no_post", responseCode = 422, jsonPath = "get_post_error.json", responseTime = 4500)})
Call<Post> getPost(@Path("id") int postId);

Now, when you are about to enqueue your call (and you want to use some of OffIt custom methods) you have to cast original Retrofit Call to MockableCall. Just like this:

Call<List<Post>> call = mApiService.getPosts();
((MockableCall<List<Post>>) call).withResponseCode(200).withResponseTime(1000).enqueue(new Callback<List<Post>>() {
    @Override
    public void onResponse(@NonNull Call<List<Post>> call, @NonNull Response<List<Post>> response) {
        // Your stuff here
    }

    @Override
    public void onFailure(@NonNull retrofit2.Call<List<Post>> call, @NonNull Throwable t) {
        // Your stuff here
    }
});

OffIt in tests

You can easily use OffIt to mock your API Responses in your tests. If you want to test everything synchronously there is nothing you need to do more than explained in previous steps. You just simply invoke a .execute() method on your calls. If you want to test your asynchronous callbacks you will need to add OffitTestRule to your tests just like this:

@Rule
public TestRule offitRule = new OffitTestRule();

And... that's it. Now all your .enqueue() methods invoked on calls will be executed on the main thread for you so don't worry about asynchronous testing!

For more detailed example, please see OffItIntegrationTests under app/src/androidTest/.. in source.

Roadmap

Please keep in mind that OffIt is still under development. There are some tweaks on the roadmap, so be ready for fixes, tweaks and new features. Some of the planned features are listed here with progress on them. Feel free to suggest improvements!

  • [ ] Network simulator
  • [ ] Headers support

Support, contact and contribution

Feel free to contact me at [email protected] for any questions. Any forms of contribution are welcome, so feel free to fork and contribute with pull requests.

License

Copyright 2018 Gabriel Samojło

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.
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].