首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在测试AsyncRestTemplate时防止预期已声明的异常

在测试AsyncRestTemplate时防止预期已声明的异常
EN

Stack Overflow用户
提问于 2017-08-14 13:01:55
回答 1查看 746关注 0票数 3

如何测试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)

应用程序聚合来自几个下游系统的数据。要求它提出几个要求。有些请求是在以后处理未来时异步提出的。其他请求通过立即调用asyncRestTemplateResponse.get()来阻塞主线程。

以下测试会导致错误:

注意:服务器是MockRestServiceServer

代码语言:javascript
复制
@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());
}
EN

回答 1

Stack Overflow用户

发布于 2017-12-20 19:39:34

根据Eric上面的说明,https://jira.spring.io/browse/SPR-16132似乎认为这是一个问题。我用的是布鲁塞尔SR5。我将属性设置为4.3.13.RELEASE,问题就解决了。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45675139

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档