I'm using Android Volley for http request/response.
com.android.volley:volley:1.0.0
I write my Android instrumented unit test in folder "androidTest".
I test my logic and UI by JUnit and Espresso. And all work fine.
One of my Espresso test:
@Test
public void addFavorite() {
onView(withId(android.support.design.R.id.search_src_text)).perform(typeText(CONTACT_SURNAME)).perform(closeSoftKeyboard());
onData(anything()).inAdapterView(withId(R.id.containerNotEmptyListView)).atPosition(0).onChildView(withText(R.string.add_favorites))
.perform(click());
onData(anything()).inAdapterView(withId(R.id.containerListView)).atPosition(0).onChildView(withText(R.string.remove_favorites))
.check(matches(isDisplayed()));
}
This Espresso test:
- Scroll in listView to contact
CONTACT_SURNAME
- Click on item
- As result start http request
- After return PRODUCTION http response check is contact now has text "remove favorite"
This Espresso test work fine. OK!
Now I want to write unit test (also in folder "androidTest") that:
Check if success response (http status = 200) then show Toast
Check if incorrect response (e.g. http status = 403) then show Dialog
So, to do this I need some tool that return to me STUB http response.
I want this tool to must have the following options:
Write test directly from Android app (in folder androidTest). Something like this:
@Test public void exactUrlOnly() { stubFor(get(urlEqualTo("/some/thing")) .willReturn(aResponse() .withHeader("Content-Type", "text/plain") .withBody("Hello world!"))); assertThat(testClient.get("/some/thing").statusCode(), is(200)); assertThat(testClient.get("/some/thing/else").statusCode(), is(403)); }
This tool can return json in http response body. Something like this:
{ "response": { "status": 200, "body": "Hello world!", "headers": { "Content-Type": "text/plain" } } }
Is anybody known such tool? Thanks.