Skip to content

YandexClassifieds/mockwebserver-dsl

Repository files navigation

MockWebServer DSL

Maven Central

MockWebServer DSL is a Kotlin library for testing Android apps. The library provides powerful DSL for MockWebServer. So, you can mock your http-client easy and concise.

Getting started

Imagine if you want to test the feed screen in your app. Your app gets feed data from server by the REST API:

GET https://myamazingapp.com/api/v1/feed?page=1

Inside your test class define rule:

@get:Rule
val webServerRule = WebServerRule {
    getFeed(page = 1, assetPath = "feed/page_1.json")
}

Where getFeed() is definition of mock:

fun Routing.getFeed(page: Int, assetPath: String) = get(
    "get feed, page $page",
    {
        pathContains("feed")
        query(StringContains("page=$page"))
    },
    response {
        setBody(asset(assetPath))
    }
)

After that, your app gets successful response with body from feed/page_1.json file.

Features 🚀

  • Add any type (GET, POST, PUT, DELETE) and any amount of requests' mocks in single or multiple definition blocks
webServerRule.routing {
    getFeed(page = 1, assetPath = "feed/page_1.json")
    getFeed(page = 2, assetPath = "feed/page_2.json")
    postOffer(id = "abc-123")
}
  • Override mocks if you need to change response in any execution point of your test
// simple feed mock
webServerRule.routing {
    getFeed(page = 1, assetPath = "feed/page_1.json")
}

// some routine...

// response changed from this point
webServerRule.routing {
    getFeed(page = 1, assetPath = "feed/page_1_viewed.json")
}
  • Use different strategies of routing:

    • CompositeRouting — mock every request if suitable routing is registered
    • OneOffRouting — one routing for one shoot. if there are two the same requests only first is mocked
    • StubRouting — mock all background requests which are not important for testing

    More details in docs.

  • Test your data layer with assertions

webServerRule.routing {
    getDraft().assertCalled()
    getOffer().assert { body(equalsTo(getOfferBody())) }
}

More features and full documentation you can find here.

Documentation 📖

Documentation

Download

androidTestImplementation("com.yandex.classifieds:mockwebserver-dsl:1.0.0")

Contributing 🤝

We welcome contributions to this project!


Contributing policy

Releasing new versions

New versions are released by hand by project maintainers. To release a new version, follow these steps:

  1. Update the version number in the build.gradle.kts file.
  2. Ensure all tests pass and the project builds successfully.
  3. Ensure you have the necessary permissions to publish to Maven Central.
  4. Run the ./gradlew centralBundle to create artifacts.
  5. Log into Maven Central.
  6. Click "Publish Component" and select build/distributions/mockwebserver-dsl-<version>.zip.
  7. After a validation period, click "Publish" to make the artifacts available.

License 📄

License

About

No description, website, or topics provided.

Resources

License

Contributing

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages