Giter Club home page Giter Club logo

android-mock-api-server's Introduction

Android Mock Api Server

It is a framework that provides local data and allows you to test your app without a backend.

Features

  • Mock response using json files, data class object or string bodies
  • Mock response sequence
  • Provide response delay time
  • Provide http response code

Installation

Inside the dependency block in the build.gradle file of your application, add this line to add the library:

repositories {
    mavenCentral()
}
dependencies {
    ...
    implementation 'com.telefonica:mock:$version'
    ...
}

Create a demo variant in order to only have json files for development purposes:

flavorDimensions "environment"

productFlavors {
    ...
    demo {
        dimension "environment"
    }
    ...
}

Create an asset folder in demo variant and place there all json files.

Provide a singleton instance of MockHelper using dependency injection:

@Provides
@Singleton
fun provideMockHelper(
    @ApplicationContext context: Context
): MockHelper = MockHelper(context)

Inject MockHelper in Application file. Then in live cycle methods, start, stop and provide mocks.

class App : Application() {
    
    ...
    @Inject
    lateinit var mockHelper: MockHelper
    ...

    override fun onCreate() {
        ...
        if (BuildConfig.DEFAULT_ENVIRONMENT == Environment.DEMO) {
            CoroutineScope(Dispatchers.IO).launch {
                mockHelper.setUp()
                mockHelper.enqueue {
                    whenever("/image.png").thenReturnFromRawFile("demo_image")
                    whenever("/?results=5").thenReturnFromFile("user_list_success_1.json")
                    whenever("/?results=10").thenReturn(Moshi.Builder().build().adapter(UserWrapperDto::class.java).toJson(DEMO_LIST))
                }
            }
        }
        ...
    }
 
    override fun onTerminate() {
        super.onTerminate()
        if (BuildConfig.DEFAULT_ENVIRONMENT == Environment.DEMO) {
            mockHelper.stopServer()
        }
    }
}

At some point in the application, where the app set the base URL to make requests, you have to check if current variant is "demo" and set the base url obtained from method:

mockHelper.getBaseUrl()

Demo App

Demo app shows a list of users in two screens, first shows five users, second shows ten users. To use mock data, select demoDebug variant:

  • The first list, is a json sequence which the first request is success and the second request is an error.
  • The second list, is a data object response.

To use real data, select preDebug variant

android-mock-api-server's People

Contributors

devpablogarcia avatar gmerinojimenez avatar tuentisre avatar jmanriquehiberus avatar

Stargazers

Arda K. avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.