0

I start my instrumental test to check my activity. When press button on Activity then call http request. So I test click

@Test
   fun click_checkRequest() {
       mockServer.enqueue(
           MockResponse()
                    .setResponseCode(200)
       )

       myContainer.click()
       val request = mockServer.takeRequest();
       Assert.assertEquals("POST", request.method)
       assertThat(request.path, CoreMatchers.containsString("/event?"))
       assertThat(
           request.body.readUtf8(), CoreMatchers.containsString( """type":1""")
       )
   }

Here log:

11-29 11:06:26.952 D/OkHttp  (15697): --> POST http://127.0.0.1:8081/event?table_token=11&device_id=111111 http/1.1
11-29 11:06:26.952 D/OkHttp  (15697): Content-Type: application/json; charset=UTF-8
11-29 11:06:26.952 D/OkHttp  (15697): Content-Length: 10
11-29 11:06:26.953 D/OkHttp  (15697): {"type":1}
11-29 11:06:26.953 D/OkHttp  (15697): --> END POST (10-byte body)
11-29 11:06:26.968 I/MockWebServer(15697): MockWebServer[8081] received request: POST /event?table_token=my_token&user=my_user&device_id=my_device HTTP/1.1 and responded: HTTP/1.1 200 OK
11-29 11:06:26.969 D/OkHttp  (15697): <-- 200 OK http://127.0.0.1:8081/event?table_token=my_token&user=my_user&device_id=my_device (16ms)
11-29 11:06:26.969 D/OkHttp  (15697): Content-Length: 0
11-29 11:06:26.969 D/OkHttp  (15697): <-- END HTTP (0-byte body)

It's work fine. The request's body content "type":1". So as result the test is pass. Nice.

But my activity (when created) )start in background periodically (every 5 seconds) the next http request:

11-29 11:59:35.843 D/OkHttp  ( 1116): --> GET http://127.0.0.1:8081/event?orgn=17 http/1.1
11-29 11:59:35.843 D/OkHttp  ( 1116): --> END GET
11-29 11:59:35.862 I/MockWebServer( 1116): MockWebServer[8081] received request: GET /event?orgn=17 HTTP/1.1 and responded: HTTP/1.1 200 OK
11-29 11:59:35.863 D/OkHttp  ( 1116): <-- 200 OK http://127.0.0.1:8081/event?orgn=17 (20ms)
11-29 11:59:35.863 D/OkHttp  ( 1116): Content-Length: 0
11-29 11:59:35.863 D/OkHttp  ( 1116): <-- END HTTP (0-byte body)

11-29 11:59:35.940 D/OkHttp  ( 1116): --> POST http://127.0.0.1:8081/event?table_token=11&device_id=111111 http/1.1
11-29 11:59:35.940 D/OkHttp  ( 1116): Content-Type: application/json; charset=UTF-8
11-29 11:59:35.940 D/OkHttp  ( 1116): Content-Length: 10
11-29 11:59:35.940 D/OkHttp  ( 1116): {"type":1}
11-29 11:59:35.940 D/OkHttp  ( 1116): --> END POST (10-byte body)

As you can see the GET http://127.0.0.1:8081/event?orgn=17 start BEFORE my test run POST http://127.0.0.1:8081/event?table_token=11&device_id=111111 And as result status 200 return to NOT my test's http request.

And as result my test is fail. Is it possible to return http response code = 200 exactly for my test ?

Alexei
  • 14,350
  • 37
  • 121
  • 240
  • the `enqueue` method takes multiple responses, add as many as you expect to be called, all with 200 – Blundell Dec 14 '19 at 13:43
  • @Blundell Can you show some example with multiple response? Thanks. I try this: mockServer.enqueue( MockResponse() .setResponseCode(200) .setResponseCode(200) ) , but it not help – Alexei Dec 15 '19 at 09:18
  • 1
    You where close. Call it twice. `mockServer.enqueue(MockResponse().setResponseCode(200)) mockServer.enqueue(MockResponse().setResponseCode(200))` It's in the readme: https://github.com/square/okhttp/tree/master/mockwebserver – Blundell Dec 15 '19 at 14:23

1 Answers1

0

I found solution (thanks for @Blundell)

  @Test
    fun click_checkRequest()() {
        //stub response
        // first request "/event?orgn=17"  - polling
        mockServer.enqueue(MockResponse().setResponseCode(200))
        // second request "/event?table_token="
        mockServer.enqueue(MockResponse().setResponseCode(200))
        waitressCallContainer.click()
        //  confirm that app made the HTTP requests you were expecting
        val firstRequest = mockServer.takeRequest()
        val secondRequest = mockServer.takeRequest()
        Assert.assertEquals("POST", secondRequest.method)
        assertThat(secondRequest.path, CoreMatchers.containsString("/event?table_token"))
        assertThat(
            secondRequest.body.readUtf8(), CoreMatchers.containsString(
                """type":${Type.CALL_WAITRESS.ordinal}"""
            )
        )
    }
Alexei
  • 14,350
  • 37
  • 121
  • 240