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

Java 防止在测试AsyncRestTemplate时预期已声明异常,java,asynchronous,mocking,resttemplate,spring-test,Java,Asynchronous,Mocking,Resttemplate,Spring Test,如何测试AsyncRestTemplate请求并避免java.lang.IllegalStateException:已声明的期望异常?对于单个测试用例,异常被不一致地抛出 Java.lang.IllegalStateException:已声明期望值 位于org.springframework.util.Assert.state(Assert.java:70) 位于org.springframework.test.web.client.SimpleRequestExpectationManage

如何测试
AsyncRestTemplate
请求并避免
java.lang.IllegalStateException:已声明的期望
异常?对于单个测试用例,异常被不一致地抛出


Java.lang.IllegalStateException:已声明期望值
位于org.springframework.util.Assert.state(Assert.java:70)
位于org.springframework.test.web.client.SimpleRequestExpectationManager.afterExpectationsDeclared(SimpleRequestExpectationManager.java:47)
位于org.springframework.test.web.client.AbstractRequestExpectationManager.validateRequest(AbstractRequestExpectationManager.java:73)
位于org.springframework.test.web.client.MockRestServiceServer$mockclienthtprequestfactory$1.executeInternal(MockRestServiceServer.java:289)
位于org.springframework.mock.http.client.MockClientHttpRequest.execute(MockClientHttpRequest.java:94)
位于org.springframework.mock.http.client.MockAsyncClientHttpRequest.executeAsync(MockAsyncClientHttpRequest.java:50)
位于org.springframework.web.client.AsyncRestTemplate.doExecute(AsyncRestTemplate.java:503)
在org.springframework.web.client.AsyncRestTemplate.execute上(AsyncRestTemplate.java:463)
位于org.springframework.web.client.AsyncRestTemplate.getForEntity(AsyncRestTemplate.java:217)
位于com.company.MainClient.getStatus(MainClient.java:151)
位于com.company.MainController.status(MainController.java:88)

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

以下测试导致错误:

注意:服务器是
MockRestServiceServer


根据上面Eric Pabst的说明,似乎认为这是一个问题。我正在使用布鲁塞尔SR5。我将属性设置为4.3.13.RELEASE,问题就消失了

FWIW,我有一个类似的失败,你可能会感兴趣:
@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());
}