- Notifications
You must be signed in to change notification settings - Fork1
Raccoon is a lightweight response mocking framework that can be easily integrated into the Android UI tests.
License
iamjosephmj/Raccoon
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
Checkout these article to get more insights about this library:
There has been different ways to mock the response from an API call, We do have a lot of good frameworksfor this purpose. But, what if we need to give back a mock response according to the request and itsparameters. That is what this library is all about. Raccoon is an interceptor plugin that will help thedeveloper to mock a response back according to the request. The library uses Reflection apis under the hoodto do the whole process.
The integration of this library is soo much compact in such a way that the developerdoesn'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.
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") }
Retrofit users need to add theRaccoonOkHttpPlugin
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 theRaccoonStub
class is initialized.
This is one of the core classes that Raccoon library is looking into. Controller class is the placewere theEndpoint
definitions are made. Developers can keep the logically relatedEndpoints
inthe sameController
@ControllerModuleclassMockController :RaccoonController() {overridefunsetup() {// 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*/ )funfetchToDoList(@Paramsheaders:Parameters):RaccoonResponse {return</* Your response object*/> .buildRaccoonResponse(statusCode=200/* STATUS code of the api response*/) }overridefuntearDown() {// clean up memory. }}
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.
This function is the best candidate to free up the memory after the endpoint point definitionexecution. This function is called after the endpoint endpoint definition is called.
The user can define any names to the endpoint definition function. The only requirement that thelibrary has is that the user should give an@RaccoonEndpoint
for the same. Take the example aboveexample to see the annotation in action.
The Endpoint Definition function has a return typeRaccoonResponse
. The developer can eitherdirectly createRaccoonResponse
object with
RaccoonResponse( statusCode=200, body=/* json string*/"{ ... }" )
or they can use the Moshi/Gson supported pojo objects. Raccoon provides a helper extensionfunctionbuildRaccoonResponse
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 inyour project.
@ControllerModule(includeControllers= [MockController2::class/* you can add any number of controllers here*/])classMockController :RaccoonController() {// your implementation ...}
Take a look at theandroidTest
implementation to get more insights on the same.
Service class helps the library to create an overall understanding about how to efficiently parsethrough controllers to fetch the endpoint.
@RaccoonServiceclassMockService :RaccoonServiceImpl() { @RaccoonControllerfunprovidesMockController()=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.
@RunWith(AndroidJUnit4::class)classMainActivityTest { @Beforefunsetup() {// setupRaccoonStub.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:Ruleval rule=ActivityTestRule(MainActivity::class.java,false,false ) @TestfunuseAppContext() {// run your test } @AfterfuntearDown() {// cleans up the memory.RaccoonStub.teatDown() }}
You can define the testRule in this way:
// Seup raccoon stubval raccoonTestRule=RaccoonTestRule {RaccoonStub.setUp(RaccoonConfig.Builder() .addService(ServiceClass::class) .build())}// Setup activity testRuleprivateval activityTestRule=ActivityTestRule(MainActivity::class.java)@get:Ruleval 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)
If part of Raccoon is not working correctly be sure to file a Github issue. In the issue provide asmany details as possible. This could include example code or the exact steps that you did so thateveryone can reproduce the issue. Sample projects are always the best way :). This makes it easyfor 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 wewill work with you to merge it in!
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.
About
Raccoon is a lightweight response mocking framework that can be easily integrated into the Android UI tests.