Wiremock基于Spring配置在Spring Boot应用程序中的设置。

huangapple go评论78阅读模式
英文:

Wiremock setup based on spring profiles in spring boot application

问题

我遇到了关于使用Spring Boot应用程序设置Wiremock的问题。

背景:
当前应用程序设置了Wiremock以用于功能测试(Spring配置文件 - test)来模拟多个下游服务的响应。

我需要为该应用程序设置性能测试环境(perf),我将使用不同的Wiremock配置来为perf环境模拟下游服务的延迟响应,因为涉及到多个下游服务,我希望仅基于Spring配置文件为perf而不是test来模拟延迟响应。Wiremock提供以下配置来实现:

"fixedDelayMilliseconds": <x milliseconds>

(参考:https://wiremock.org/docs/simulating-faults/)

但是,如果我将此参数包含在当前的Wiremock配置中,它也将应用于功能测试(Spring配置文件test)。请建议一种仅针对perf而不是test基于Spring配置文件添加此延迟的方法。

我考虑在perf环境中实现模拟的服务实现并在那里添加响应延迟,但如果可能的话,我想通过配置来实现这一点。

英文:

I have run into a probelm with Wiremock setup with spring boot application.

>Background:
Current application setup has wiremock implemented for functional tests(spring profile - test) to mock responses for multiple downstream services.

I need to setup Performance Test Environment (perf) for the application, I will be using different wiremock configurations for perf environment .
As there are multiple downstream services involved, I want to simulate delayed response from downstream services to get accurate results. Wiremock provides us with following configuration to do that:

> &quot;fixedDelayMilliseconds&quot;: &lt;x milliseconds&gt;

(ref : https://wiremock.org/docs/simulating-faults/)

But if I include this param in my current wiremock configuration, it will also get applied to functional test(spring profile test). Please suggest a way to add this delay based on spring profile only for perf but not for test.

I am thinking to implement mocked services implementation for perf env and add response delays there, but I want to achieve this through configurations, if possible.

答案1

得分: 1

你可以在激活配置文件为perf时创建一个WireMockConfigurationCustomizer,并注册一个设置延迟的扩展,而不是直接在你的Wiremock存根定义中设置延迟。

@Configuration
public class WiremockConfig {

    @Bean
    @Profile("perf")
    public WireMockConfigurationCustomizer wiremockConfig() {
        return config -> config.extensions(
                new ResponseDefinitionTransformer() {
                    @Override
                    public ResponseDefinition transform(Request request, ResponseDefinition response, FileSource fileSource, Parameters parameters) {
                        return ResponseDefinitionBuilder.like(response).withFixedDelay(2000).build();
                    }

                    @Override
                    public String getName() {
                        return "inject-delay-transformer";
                    }
                }
        );
    }
}
英文:

You can create a WireMockConfigurationCustomizer when profile perf is active and register an extension which is setting that delay instead of setting the delay directly in your wiremock stub definitions.

@Configuration
public class WiremockConfig {

    @Bean
    @Profile(&quot;perf&quot;)
    public WireMockConfigurationCustomizer wiremockConfig() {
        return config -&gt; config.extensions(
                new ResponseDefinitionTransformer() {
                    @Override
                    public ResponseDefinition transform(Request request, ResponseDefinition response, FileSource fileSource, Parameters parameters) {
                        return ResponseDefinitionBuilder.like(response).withFixedDelay(2000).build();
                    }

                    @Override
                    public String getName() {
                        return &quot;inject-delay-transformer&quot;;
                    }
                }
        );
    }
}

huangapple
  • 本文由 发表于 2023年4月13日 19:15:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/76004783.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定