0

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:

  1. Scroll in listView to contact CONTACT_SURNAME
  2. Click on item
  3. As result start http request
  4. 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:

  1. Check if success response (http status = 200) then show Toast

  2. 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:

  1. 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));
    }
    
  2. 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.

Bertrand Martel
  • 42,756
  • 16
  • 135
  • 159
Alexei
  • 14,350
  • 37
  • 121
  • 240

0 Answers0