orhanobut/mockwebserverplus is a Java project with 90 stars in the Mobile space. ✔️ OkHttp mockwebserver with fixtures extension
Snapshot summary built from the project's own GitHub metadata — there's no written TopGit review yet. The page will update automatically when a full review is published.
WHY NO REVIEW YET
TopGit writes full reviews for the most-starred, most-requested repositories. This page is a snapshot until then — see the READ ME tab for the original README in full.
MockWebServer is a great tool for mocking network requests/responses.
In order to add response, you need to set MockResponse body along with all
properties you need
Imagine it with huge json responses. It will obscure the method content and will be barely readable.
Solution
In order to make it more readable, you can use fixtures. Move away your response to the fixtures and just reference them.
MockWebServerPlus is a wrapper which contains MockWebServer with fixtures feature.
Create a fixture yaml file under resources/fixtures
statusCode : 200 // as the name says
delay: 0 // delays the response
headers: // adds to the response
- 'Auth:auth'
- 'key:value'
body: 'common/body_file.json' // can be any path under /fixtures folder
// or inline
body: > // can be any text, json, plain etc. Use > letter for scalar text
{
"array": [
1,
2,
3
],
"boolean": true,
"null": null,
"number": 123,
"object": {
"a": "b",
"c": "d",
"e": "f"
},
"string": "Hello World"
}
Use the file name to reference it. That's it!
@Rule public MockWebServerPlus server = new MockWebServerPlus();
@Test public void readableTest() {
server.enqueue("foo_success");
// execute request
// assert
// verify
}
Use the generated Fixtures.java to reference your fixtures. Read the Generate Fixtures.java part
You may wish to use a custom dispatcher with the mock web server.
Fixtures can be used directly inside a Dispatcher:
new Dispatcher() {
@Override public MockResponse dispatch(RecordedRequest request) throws InterruptedException {
return Fixture.parseFrom("simple).toMockResponse();
}
}
Generate Fixtures.java
You can always use plain text to reference your fixtures.
server.enqueue("foo_success");
but you can also generate Fixtures.java file to have all of them with a task. This will make your code more type-safe.
Put the following task into your build.gradle file and execute it when you add/modify your fixture resources.
Above solution will generate Fixtures.java when you execute it manually. But you might forget to execute it, you can
make it dependent for any task to make it automated. Whenever preBuild is executed, it will also execute this task
preBuild.dependsOn generateFixtures
Install
testCompile 'com.orhanobut:mockwebserverplus:2.0.0'
// This is optional, but in order to be up-to-date with OkHttp changes, you can use the latest version
testCompile 'com.squareup.okhttp3:mockwebserver:3.7.0'
Other proxy methods
MockWebServerPlus.server() // returns MockWebServer instance
MockWebServerPlus.takeRequest() // returns RecordedRequest
MockWebServerPlus.url(String path) // returns url to execute
MockWebServerPlus.setDispatcher(Dispatcher dispatcher) // any custom dispatcher
MockWebServerPlus.enqueue(SocketPolicy socketPolicy) // Useful for network errors, such as DISCONNECT etc
For non-android modules, you may need to add the following tasks to copy your resources into classes dir
task copyTestResources(type: Copy) {
from sourceSets.test.resources
into sourceSets.test.output.classesDir
}
How it works
Also notice that accessing sourceSets should be without android.
Credits
MockWebServer from Square
License
Copyright 2017 Orhan Obut
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
How active is development on orhanobut/mockwebserverplus?
The most recent commit recorded on orhanobut/mockwebserverplus was 9.4 years ago, based on the GitHub push timestamp. The repository has 19 forks — one of the better signals of community interest.
How does orhanobut/mockwebserverplus compare to other Mobile projects?
orhanobut/mockwebserverplus is tracked by TopGit in the Mobile category, with 90 GitHub stars and written in Java. Browse the Mobile topic page on TopGit to compare it against similar projects by stars and activity.
How many stars does orhanobut/mockwebserverplus have?
orhanobut/mockwebserverplus has 90 GitHub stars — refresh the page for the live number, or check github.com/orhanobut/mockwebserverplus. TopGit mirrors GitHub's count but does not claim minute-by-minute accuracy.
Is orhanobut/mockwebserverplus open source?
Yes — orhanobut/mockwebserverplus ships under the Apache-2.0 license, which makes its source code freely readable (and, depending on license terms, forkable and reusable). Source: github.com/orhanobut/mockwebserverplus.
What is orhanobut/mockwebserverplus?
orhanobut/mockwebserverplus (orhanobut/mockwebserverplus) is a Java project on GitHub. From the project's own README: ✔️ OkHttp mockwebserver with fixtures extension
Where do I read more about orhanobut/mockwebserverplus?
This TopGit page is a snapshot — the READ ME tab shows the project's own README content (links stripped, images preserved). The GitHub repository at github.com/orhanobut/mockwebserverplus is the definitive source.
Read full README in the tab above.
Still deciding about mockwebserverplus?
One click hands the question to an AI along with this page — see what it says about mockwebserverplus.