Search code examples
javaspringspring-bootperformance-testingwiremock

Wiremock setup based on spring profiles in spring boot application


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.


Solution

  • 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";
                        }
                    }
            );
        }
    }