无法模拟长时间存根响应

时间:2019-05-05 12:12:42

标签: android-espresso mockwebserver

Android Studio 3.3。改造2。 我想在单击按钮startButton之后模拟较长的响应时间(5秒)。 这是我的意式浓缩咖啡的测试:

import okhttp3.mockwebserver.MockResponse
import okhttp3.mockwebserver.MockWebServer
import org.junit.After
import org.junit.Before
import java.util.concurrent.TimeUnit
@RunWith(AndroidJUnit4::class)
class AddTraderActivityTest {

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

    @Before
    fun setup() {
        mockServer = MockWebServer()
        mockServer.start(8081)
    }

    @After
    @Throws
    fun tearDown() {
        mockServer.shutdown()
    }

  @Test
    fun buttonStart_click_delayResponse() {
        // stub response
        mockServer.enqueue(MockResponse()
                .setResponseCode(200)
                .setBodyDelay(5, TimeUnit.SECONDS))
        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())        
    }
}

如您所见,我使用setBodyDelay模拟长时间响应。 但是,当我开始测试buttonStart_click_delayResponse时,mockwebserver立即返回存根响应。返回存根响应之前不要等待5秒

此处记录:

05-05 14:54:59.263 D/OkHttp  (14337): --> GET http://127.0.0.1:8081/trader/create?base=Base%20test&quote=Quote%20test&sender=myproject.android.debug_0.0.8&key=541405052019 http/1.1
05-05 14:54:59.279 I/MockWebServer(14337): MockWebServer[8081] received request: GET /trader/create?base=Base%20test&quote=Quote%20test&sender=myproject.android.debug_0.0.8&key=541405052019 HTTP/1.1 and responded: HTTP/1.1 200 OK
05-05 14:54:59.281 D/OkHttp  (14337): <-- 200 OK http://127.0.0.1:8081/trader/create?base=Base%20test&quote=Quote%20test&sender=myproject.android.debug_0.0.8&key=541405052019 (17ms, 0-byte body)

0 个答案:

没有答案