英文:
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:
> "fixedDelayMilliseconds": <x milliseconds>
(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("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";
}
}
);
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论