1

我有一个服务(A)调用另一个服务(B),我正在用 Wiremock 模拟它。我想做的是做一个测试:

  1. 如果我处于录音模式,请记录呼叫 A -> B 并将它们放入文件中
  2. 如果我处于非录音模式,则捕获呼叫 A -> B 并将它们与预先录制的模拟进行比较(这就像回归测试)

我当前的结构是 BMockRule.java

public class BMockRule extendsWireMockClassRule {


    public BMockRule(Options options) {
        super(options);
    }

    @Override
    protected void before() {
        super.before();
        this.stub();
    }

    @Override
    public void stub() {

        this.stubFor(requestMatching(request ->
                MatchResult.of(request.getUrl().contains("/request")))
                .willReturn(aResponse().withStatus(200))
        );
    }
}

我的测试里面看起来像

public class QuoteCachePublisherIntTest {
    @ClassRule
    public static DropwizardAppRule<MicroServiceConfiguration> rule =
            new DropwizardAppRule<>(
                    MicroServiceApplication.class,
                    resourceFilePath("test_config.yml"));

    private QuoteCachePublisher publisher;

    @ClassRule
    public static BasicWiremockClassRule serviceB =
            new BMockRule(options().port(5100)).withStatus(200);

    @Test
    public void test_Request() {

        if(record) {
            WireMock.startRecording("http://localhost:5100");
        } 

        publisher.publish(parameters);

        if(record) {
            SnapshotRecordResult recordedMappings = WireMock.stopRecording()
            <write-stubs-to-file>
        } else {
            SnapshotRecordResult recordedMappings = <read-stubs-from-file>
        }

        // verify statements that compare the actual calls vs the recorded mappings
    }

问题是,当我进行录制时,存根不起作用。有没有一种简单的方法可以做到这一点。

谢谢 :)

4

0 回答 0