You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

WireMock调用成功后录制内容为空问题咨询

WireMock录制功能疑问:快照录制为何无法捕获请求?

我查阅了WireMock文档:

快照录制实际上是“事后录制”。它无需在特定节点启动录制,而是允许将WireMock已接收的请求转换为Stub Mapping。

按我的理解,下面的测试应该能通过:

package com.example.gatewaydemo.misc;

import java.util.List;

import com.github.tomakehurst.wiremock.junit5.WireMockRuntimeInfo;
import com.github.tomakehurst.wiremock.junit5.WireMockTest;
import com.github.tomakehurst.wiremock.stubbing.StubMapping;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import reactor.core.publisher.Mono;
import reactor.test.StepVerifier;

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.reactive.function.client.WebClient;

import static com.github.tomakehurst.wiremock.client.WireMock.get;
import static com.github.tomakehurst.wiremock.client.WireMock.ok;
import static com.github.tomakehurst.wiremock.client.WireMock.stubFor;
import static org.assertj.core.api.Assertions.assertThat;

@WireMockTest
public class GenericTest {
    int port;

    @BeforeEach
    void setUp(WireMockRuntimeInfo info) {
        port = info.getHttpPort();
    }

    @Test
    void test(WireMockRuntimeInfo info) {
        stubFor(get("/test").willReturn(ok()));

        Mono<ResponseEntity<Void>> response = WebClient.builder()
                .baseUrl("http://localhost:%d".formatted(port))
                .build()
                .get()
                .uri("/test")
                .retrieve()
                .toBodilessEntity();

        // 此断言通过
        StepVerifier.create(response.map(ResponseEntity::getStatusCode))
                .expectNext(HttpStatus.OK)
                .verifyComplete();

        // 此断言失败:stubMappings为空
        List<StubMapping> stubMappings = info.getWireMock().takeSnapshotRecording();
        assertThat(stubMappings).isNotEmpty();
    }
}

但实际并未通过。在实际测试中,我需要断言WireMock模拟端点接收到的请求,原本期望获取录制的端点调用列表后,执行类似如下操作来校验请求体:

String actualBody = (String) stubMappings.get(0).getRequest().getBodyPatterns().get(0).getValue();
assertThat(actualBody).isEqualTo(expectedBody);

我了解startRecording和stopRecording方法,希望测试代码简洁且可读,所以尝试了这种方式,但同样无效,说明问题并非特定于快照录制:

@Test
void test(WireMockRuntimeInfo info) {
    stubFor(get("/test").willReturn(ok()));
    WireMock wireMock = info.getWireMock();
    wireMock.startStubRecording();

    Mono<ResponseEntity<Void>> response = WebClient.builder()
            .baseUrl("http://localhost:%d".formatted(port))
            .build()
            .get()
            .uri("/test")
            .retrieve()
            .toBodilessEntity();

    // 此断言通过
    StepVerifier.create(response.map(ResponseEntity::getStatusCode))
            .expectNext(HttpStatus.OK)
            .verifyComplete();

    // 此断言失败:snapshotRecordResult.getStubMappings()为空
    SnapshotRecordResult snapshotRecordResult = wireMock.stopStubRecording();
    assertThat(snapshotRecordResult.getStubMappings()).isNotEmpty();
}

我搜索到一个使用ServeEvent和为存根分配ID的方案来捕获请求,想问:

  • 这是不是捕获WireMock请求的唯一方式?
  • 如果是,那WireMock的录制功能到底用于什么场景?

内容的提问来源于stack exchange,提问作者Powet

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.27 03:27:04