我´我现在正在做噩梦测试我的spring webclient代码。给出以下简单方法
private String request(String uri, String body) {
return webClient
.method(HttpMethod.POST)
.uri(uri)
.body(Mono.just(body), String.class)
.retrieve()
.bodyToMono(String.class)
.block();
}
以及以下单元测试:
@Before
public void setUp() throws IOException {
baseUrl = "http://localhost:%s";
mockWebServer = new MockWebServer();
mockWebServer.start();
int mockWebServerPort = mockWebServer.getPort();
baseUrl = String.format(baseUrl, mockWebServerPort);
webClient = WebClient.create();
}
@After
public void shutdown() throws IOException {
mockWebServer.shutdown();
}
@Test
public void testPost() throws InterruptedException {
String body = "Hello";
givenMockResponse(response -> {
response.setResponseCode(200);
response.setBody(body);
});
request(baseUrl, body);
RecordedRequest recordedRequest = mockWebServer.takeRequest();
checkRequest(recordedRequest);
}
当我通过intellij运行测试时它通过了,但是当我´m使用maven时会引发以下异常:
reactor.core.Exceptions$ReactiveException: java.util.concurrent.TimeoutException: Did not observe any item or terminal signal within 0ms in 'flatMap' (and no fallback has been configured)
当我启动spring启动应用程序并手动测试它时,request方法也可以正常运行。我的项目使用springboot2.3.1,mavensurefire-plugin:3.0.0-m3,jdk 11和用于使用okhttp3 mockwebserver 4.0.1测试junit4。你知道这个问题是由什么引起的吗?
暂无答案!
目前还没有任何答案,快来回答吧!