我开始我的仪器测试来检查我的活动。当按下 Activity 上的按钮然后调用 http 请求。所以我测试点击
@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""")
)
}
这里日志:
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)
它工作正常。请求的正文内容“type”:1”。因此测试通过了。很好。
但我的活动(创建时))在后台定期(每 5 秒)启动下一个 http 请求:
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)
如您所见,在我的测试运行 POST之前GET http://127.0.0.1:8081/event?orgn=17
开始
,结果状态 200 返回不是我的测试的 http 请求。http://127.0.0.1:8081/event?table_token=11&device_id=111111
结果我的测试失败了。是否可以准确地为我的测试返回 http 响应代码 = 200 ?