IntelliJ 运行 JUnit4 测试没有问题,maven 测试出现异常

问题描述

我目前正在做噩梦来测试我的 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 运行测试时它通过了,但是当我使用 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 Boot 应用程序并手动测试时,请求方法也运行良好。我的项目使用 Spring Boot 2.3.1、maven-surefire-plugin:3.0.0-M3、JDK 11,并使用 okhttp3 mockwebserver 4.0.1 测试 JUnit4。你知道这个问题可能是由什么引起的吗?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)