【发布时间】:2016-08-04 21:20:14
【问题描述】:
我正在做一个集成测试,我调用一个将文件发送到服务器的 POST 请求。在 POST 之后,我立即调用一个 GET 请求来检索文档并获得一个响应,该响应为我提供了该文档的属性。但是,有时我会收到 404 错误,说文档不存在,直到我在几秒钟后再次运行它。
我认为 POST 还没有完成处理,所以我实现了一个 Thread.sleep 来等待 10 秒,但这种方式似乎并不好,因为它可能等待的时间比它需要的时间长,或者可能等待的时间不够长。是否有某种“隐式等待”允许 POST 在请求 GET 之前完成处理?
下面是代码sn-p:
@Test
public void PostDocumentThenCheckIfDocumentExistThenRemove() throws IOException, InterruptedException {
try {
String str = fileToStringProcessing("C:/Users/Linh/Desktop/file.xml");
ResponseEntity<Message> postResponse = getRestTemplate().exchange(getUri() + "documents", HttpMethod.POST, new HttpEntity(str, getHeaders()), Message.class);
Thread.sleep(10000);
ResponseEntity<Account> getResponse = getRestTemplate().exchange(getUri() + "account/7452616052/documents?start=2015-01-01&end=2016-03-31", HttpMethod.GET, getHttpEntity(), Account.class);
ResponseEntity<Message> deleteResponse = getRestTemplate().exchange(getUri() + "documents/file.xml", HttpMethod.DELETE, getHttpEntity(), Message.class);
assertThat(postResponse.getStatusCode(), is(HttpStatus.CREATED));
assertThat(getResponse.getStatusCode(), is(HttpStatus.OK));
assertThat(deleteResponse.getStatusCode(), is(HttpStatus.OK));
}catch(HttpClientErrorException e) {
fail("Error! Status code " + e.getStatusCode());
}
}
这是控制台中的 404 错误。您可以在此处看到 POST 成功但 GET 不成功。之后执行 GET,几秒钟后,将显示成功
11:56:32.700 [main] DEBUG o.s.web.client.RestTemplate - POST request for "https://dpdev.billing.com/tf/dp/documents" resulted in 201 (Created)
11:56:32.700 [main] DEBUG o.s.web.client.RestTemplate - Reading [com.digitalplatform.model.Message] as "application/xml" using [org.springframework.http.converter.xml.Jaxb2RootElementHttpMessageConverter@1dac5ef]
11:56:32.904 [main] DEBUG o.s.web.client.RestTemplate - Created GET request for "https://dpdev.billing.com/tf/dp/account/7452616052/documents?start=2015-01-01&end=2016-03-31"
11:56:32.914 [main] DEBUG o.s.web.client.RestTemplate - Setting request Accept header to [application/xml, text/xml, application/json, application/*+xml, application/*+json]
11:56:33.690 [main] DEBUG o.s.web.client.RestTemplate - GET request for "https://dpdev.billing.com/tf/dp/account/7452616052/documents?start=2015-01-01&end=2016-03-31" resulted in 404 (Not Found); invoking error handler
GET 和 DELETE 请求在 404 之后:
12:00:21.383 [main] DEBUG o.s.web.client.RestTemplate - Created GET request for "https://dpdev.billing.com/tf/dp/account/7452616052/documents?start=2015-01-01&end=2016-03-31"
12:00:21.444 [main] DEBUG o.s.web.client.RestTemplate - Setting request Accept header to [application/xml, text/xml, application/json, application/*+xml, application/*+json]
12:00:23.176 [main] DEBUG o.s.web.client.RestTemplate - GET request for "https://dpdev.billing.com/tf/dp/account/7452616052/documents?start=2015-01-01&end=2016-03-31" resulted in 200 (OK)
12:00:23.176 [main] DEBUG o.s.web.client.RestTemplate - Reading [com.digitalplatform.model.Account] as "application/xml" using [org.springframework.http.converter.xml.Jaxb2RootElementHttpMessageConverter@37271612]
12:00:23.380 [main] DEBUG o.s.web.client.RestTemplate - Created DELETE request for "https://dpdev.billing.com/tf/dp/documents/file.xml"
12:00:23.381 [main] DEBUG o.s.web.client.RestTemplate - Setting request Accept header to [application/xml, text/xml, application/json, application/*+xml, application/*+json]
12:00:25.120 [main] DEBUG o.s.web.client.RestTemplate - DELETE request for "https://dpdev.billing.com/tf/dp/documents/file.xml" resulted in 200 (OK)
12:00:25.120 [main] DEBUG o.s.web.client.RestTemplate - Reading [com.digitalplatform.model.Message] as "application/xml" using [org.springframework.http.converter.xml.Jaxb2RootElementHttpMessageConverter@37271612]
【问题讨论】:
标签: java spring rest testing junit