在测试 AsyncRestTemplate 时防止预期已经声明异常
Posted
技术标签:
【中文标题】在测试 AsyncRestTemplate 时防止预期已经声明异常【英文标题】:Prevent expectations already declared exception when testing AsyncRestTemplate 【发布时间】:2018-01-22 07:53:37 【问题描述】:如何测试AsyncRestTemplate
请求并避免java.lang.IllegalStateException: Expectations already declared
异常?单个测试用例的异常抛出不一致。
Java.lang.IllegalStateException: Expectations already declared
at org.springframework.util.Assert.state(Assert.java:70)
at org.springframework.test.web.client.SimpleRequestExpectationManager.afterExpectationsDeclared(SimpleRequestExpectationManager.java:47)
at org.springframework.test.web.client.AbstractRequestExpectationManager.validateRequest(AbstractRequestExpectationManager.java:73)
at org.springframework.test.web.client.MockRestServiceServer$MockClientHttpRequestFactory$1.executeInternal(MockRestServiceServer.java:289)
at org.springframework.mock.http.client.MockClientHttpRequest.execute(MockClientHttpRequest.java:94)
at org.springframework.mock.http.client.MockAsyncClientHttpRequest.executeAsync(MockAsyncClientHttpRequest.java:50)
at org.springframework.web.client.AsyncRestTemplate.doExecute(AsyncRestTemplate.java:503)
at org.springframework.web.client.AsyncRestTemplate.execute(AsyncRestTemplate.java:463)
at org.springframework.web.client.AsyncRestTemplate.getForEntity(AsyncRestTemplate.java:217)
at com.company.MainClient.getStatus(MainClient.java:151)
at com.company.MainController.status(MainController.java:88)
应用程序聚合来自多个下游系统的数据。要求它提出几个请求。一些请求是异步发出的,Future 稍后会被处理。其他请求通过立即调用asyncRestTemplateResponse.get()
阻塞主线程。
以下测试导致错误:
注意:服务器是
MockRestServiceServer
@Test
public void statusTest() throws Exception
cServer.expect(once(),requestTo("http://localhost:8080/heartbeat"))
.andRespond(withSuccess(cStatus, MediaType.APPLICATION_JSON));
cpServer.expect(once(),requestTo("http://localhost:8081/status"))
.andRespond(withSuccess(cpStatus, MediaType.APPLICATION_JSON));
tServer.expect(once(),requestTo("http://localhost:3030/check"))
.andRespond(withSuccess(tStatus, MediaType.TEXT_PLAIN));
tServer.expect(once(),requestTo("http://localhost:3031/check"))
.andRespond(withSuccess(tStatus, MediaType.TEXT_PLAIN));
tServer.expect(once(),requestTo("http://localhost:3032/check"))
.andRespond(withSuccess(tStatus, MediaType.TEXT_PLAIN));
mockMvc.perform(get("/status").with(user(USERNAME).password(PASSWORD).roles("T_CLIENT")))
.andDo(print()).andExpect(status().isOk())
.andExpect(jsonPath("$.code").value("200"))
.andExpect(jsonPath("$.appDescription").value("Main Service"))
.andExpect(jsonPath("$.gateways[?(@.responseCode =~ /200/)]").isArray());
//Test without basic auth
mockMvc.perform(get("/status"))
.andDo(print()).andExpect(status().isUnauthorized());
【问题讨论】:
FWIW,我遇到了您可能感兴趣的类似故障:jira.spring.io/browse/SPR-16132 【参考方案1】:根据 Eric Pabst 的上述说明,https://jira.spring.io/browse/SPR-16132 似乎认为这是一个问题。我正在使用布鲁塞尔-SR5。我将
【讨论】:
以上是关于在测试 AsyncRestTemplate 时防止预期已经声明异常的主要内容,如果未能解决你的问题,请参考以下文章
将 OAuth2RestTemplate 公开为 AsyncRestTemplate
无论如何从 AsyncResttemplate 获取 http.client.requests 指标?