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.
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.
- 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.
androidTestImplementation("com.yandex.classifieds:mockwebserver-dsl:1.0.0")We welcome contributions to this project!
New versions are released by hand by project maintainers. To release a new version, follow these steps:
- Update the version number in the
build.gradle.ktsfile. - Ensure all tests pass and the project builds successfully.
- Ensure you have the necessary permissions to publish to Maven Central.
- Run the
./gradlew centralBundleto create artifacts. - Log into Maven Central.
- Click "Publish Component" and select
build/distributions/mockwebserver-dsl-<version>.zip. - After a validation period, click "Publish" to make the artifacts available.