0

Adnroid studio 3.3.

In my app I use Retrofit to do http request. Also I use MockWebServer to return stub response.

On activity when I click button I start async http request (by Retrofit) and wait callback method to return response. Here Espresso's test:

@RunWith(AndroidJUnit4::class)
class AddTraderActivityTest {
@get:Rule
    var addTraderIntentTestRule: IntentsTestRule<AddTraderActivity> = IntentsTestRule(AddTraderActivity::class.java)

    @Before
    fun setup() {
        mockServer = MockWebServer()
        mockServer.start(8081)
        Debug.d(TAG, "SUCCCESS_START_MOCKWEBSRVER")
    }

  @Test
fun buttonStart_click_longResponse() {
    // stub response
    mockServer.enqueue(MockResponse()
            .setResponseCode(200)
            .setBody(FileUtil.getStringFromFile(context, "add_trader_success_200.json"))
            .setBodyDelay(5000, TimeUnit.MILLISECONDS))

    onView(withId(R.id.baseTextInputEditText))
            .perform(typeText(BASE_TEST))
    onView(withId(R.id.quoteTextInputEditText))
            .perform(typeText(QUOTE_TEST))
    onView(withId(R.id.startButton))
            .perform(click())
    onView(withText(R.id.containerProgressBarLayout))
            .check(matches(isDisplayed()))
}

But problem is when execute perform(click() the method check is not call until not get stub response (after 5 seconds).

But I need call method check immediately after perform(click() method. Because I need to check is containerProgressBarLayout is isDisplayed() while not return stub response. I need to check my view DURING loading data

How I can do this?

Alexei
  • 14,350
  • 37
  • 121
  • 240

1 Answers1

0

You can use Espresso idling resources.

An idling resource represents an asynchronous operation whose results affect subsequent operations in a UI test. By registering idling resources with Espresso, you can validate these asynchronous operations more reliably when testing your app.

Also, there is a library for Okhttp, you can check it out here

Saeed Masoumi
  • 8,746
  • 7
  • 57
  • 76
  • I need to check my view DURING loading data. Not after data was loaded. Is it possible by Espresso idling resources? – Alexei May 06 '19 at 13:20
  • @Alexei, I don't know how you implement your mock server, it depends. But try to create an idling resource and always return true inside `isIdleNow` so It maybe skips waiting and goes to the next step. – Saeed Masoumi May 06 '19 at 20:39
  • I use this mockwebserver https://github.com/square/okhttp/tree/master/mockwebserver – Alexei May 06 '19 at 21:22
  • isIdleNow = true is not help. Method "onView(withId(R.id.startButton)).perform(click())" "freeze" application. And method "check" call AFTER 5 seconds. – Alexei May 08 '19 at 11:37