Giter Site home page Giter Site logo

iamjosephmj / raccoon Goto Github PK

View Code? Open in Web Editor NEW
54.0 1.0 1.0 397 KB

Raccoon is a lightweight response mocking framework that can be easily integrated into the Android UI tests.

License: MIT License

Kotlin 100.00%
mock android kotlin-android kotlin-library uitest espresso-tests junit

raccoon's Introduction

Raccoon Logo

Raccoon

Medium Articles

Checkout these article to get more insights about this library:

Why Raccoon?

There has been different ways to mock the response from an API call, We do have a lot of good frameworks for this purpose. But, what if we need to give back a mock response according to the request and its parameters. That is what this library is all about. Raccoon is an interceptor plugin that will help the developer to mock a response back according to the request. The library uses Reflection apis under the hood to do the whole process.

Insights

The integration of this library is soo much compact in such a way that the developer doesn't need to take much time for the process. We basically need to make main 3 additions:

  • Add the Interceptor Plugin
  • Add Service class in the AndroidTest directory
  • Add Controller class and the endpoint definition in the AndroidTest directory.

Gradle

Add the following to your project's root build.gradle.kts file

allprojects {
  repositories {
	maven ("https://jitpack.io")
  }
}

Add the following to your project's build.gradle.kts file

dependencies {
  
  // Other dependencies
  implementation("com.github.iamjosephmj:Raccoon:1.0.7")
     
}

Basic usage

Add interceptor plugin

Retrofit

Retrofit users need to add the RaccoonOkHttpPlugin as the interceptor

  val okHttpClient = OkHttpClient.Builder()
            .addInterceptor(RaccoonOkHttpPlugin())
            .build()

  val retrofit = Retrofit.Builder()
            .client(okHttpClient)
            .build()

Adding this plugin to the real project doesn't hurt until the RaccoonStub class is initialized.

Add Controller Class

This is one of the core classes that Raccoon library is looking into. Controller class is the place were the Endpoint definitions are made. Developers can keep the logically related Endpoints in the same Controller

@ControllerModule
class MockController : RaccoonController() {

    override fun setup() {
        // Do the DI related stuff
    }

    @RaccoonEndpoint(
        endpoint = "your endpoint",
        responseTime = 100, /* delay in api response delivery */
        RaccoonRequestType.GET /* user can define their request types here */
    )
    fun fetchToDoList(@Params headers: Parameters): RaccoonResponse {
        return </* Your response object */>
        .buildRaccoonResponse(statusCode = 200 /* STATUS code of the api response */)
    }


    override fun tearDown() {
        // clean up memory.
    }
}

setUp()

If user wishes to import some objects via DI, this function will be entry point to do the same. This function is called before the endpoint endpoint definition is called.

tearDown()

This function is the best candidate to free up the memory after the endpoint point definition execution. This function is called after the endpoint endpoint definition is called.

Endpoint Definition function

The user can define any names to the endpoint definition function. The only requirement that the library has is that the user should give an @RaccoonEndpoint for the same. Take the example above example to see the annotation in action.

The Endpoint Definition function has a return type RaccoonResponse. The developer can either directly create RaccoonResponse object with

    RaccoonResponse(
        statusCode=200,
        body =/* json string */ "{ ... }"
    )

or they can use the Moshi/Gson supported pojo objects. Raccoon provides a helper extension function buildRaccoonResponse to do the same

 return </* Your response object */>
        .buildRaccoonResponse(statusCode = 200 /* STATUS code of the api response */)

You can also include multiple controllers in single controller class to maintain modularity in your project.

@ControllerModule(includeControllers = [MockController2::class /* you can add any number of controllers here */])
class MockController : RaccoonController() {

    // your implementation ...
}

Take a look at the androidTest implementation to get more insights on the same.

Add Service Class

Service class helps the library to create an overall understanding about how to efficiently parse through controllers to fetch the endpoint.

@RaccoonService
class MockService : RaccoonServiceImpl() {

    @RaccoonController
    fun providesMockController() = MockController::class
}

This developer should:

  • Extend the RaccoonServiceImpl class
  • Add @RaccoonService as the annotation for the Service class.
  • Add @RaccoonController as the annotation for mock controller class provider function.

Test class implementation

@RunWith(AndroidJUnit4::class)
class MainActivityTest {

    @Before
    fun setup() {
       // setup
        RaccoonStub.setUp(
                  RaccoonConfig.Builder()
                      .addService(MockService::class)
                      .addService(MockService2::class)
                      .addService(MockService3::class)
                      .setParserType(GsonPlugin())
                      .build()
              )
        /*
         * The developer can also use {@see MoshiPlugin} as the parserType as per the project
         * requirements
         */
    }

    /**
     * You should not launch the activity before the RaccoonStub initialization.
     * There can be scenarios where the app calls the API on launch. In such cases only launch the
     * Activity inside the test function
     */
    @get:Rule
    val rule = ActivityTestRule(
        MainActivity::class.java, false, false
    )

    @Test
    fun useAppContext() {
        // run your test
    }

    @After
    fun tearDown() {
       // cleans up the memory.
        RaccoonStub.teatDown()
    }

}

Raccoon TestRule

You can define the testRule in this way:

 
// Seup raccoon stub
val raccoonTestRule = RaccoonTestRule {
     RaccoonStub.setUp(
     	RaccoonConfig.Builder()
        	.addService(ServiceClass::class)
                .build()
	)
}
 
// Setup activity testRule 
private val activityTestRule = ActivityTestRule(MainActivity::class.java)

@get:Rule
val chain: RuleChain = RuleChain
	/*
	 * Always give raccoon testrule before the activity testRule, because this can be helpfull
	 * if you app does an API call at the launch of an activity.
         */
	.outerRule(raccoonTestRule)
        .around(activityTestRule)
    

Contribution, Issues or Future Ideas

If part of Raccoon is not working correctly be sure to file a Github issue. In the issue provide as many details as possible. This could include example code or the exact steps that you did so that everyone can reproduce the issue. Sample projects are always the best way :). This makes it easy for our developers or someone from the open-source community to start working!

If you have a feature idea submit an issue with a feature request or submit a pull request and we will work with you to merge it in!

Contribution guidelines

Contributions are more than welcome!

  • You should make sure that all the tests are working properly.
  • You should raise a PR to develop branch
  • Before you raise a PR please make sure your code had no issue from Android studio lint analyzer.

Please Share & Star the repository to keep me motivated.

raccoon's People

Contributors

iamjosephmj avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Forkers

joaoppedrosa

raccoon's Issues

Create test rule for Raccoon unit and tear down

Looking at the readme, it might be beneficial to add a junit test rule to setup and teardown Raccoon without adding the extra methods in the test class. It would also allow you to wrap the raccoon test rule around the ActivityTestRule and support launching the activity default.

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.