**Last Update**: 25.03.2023 *** **We can use this variant when upgrading *WireMock* version to 2.31.0 is not possible** (for whatever reason) and/or we want to bind the *WireMock* server lifecycle to a Spring `TestContext`. ### Dependencies ```xml <dependency> <groupId>com.github.tomakehurst</groupId> <artifactId>wiremock-jre8</artifactId> <version>2.35.0</version> <scope>test</scope> </dependency> ``` ### Setup Spring executes every configured `ApplicationContextInitializer` upon starting the `ApplicationContext`. As part of this initializer, we can start the WireMock server, register the server as a bean inside the Spring context and override the configuration value for our `todo_base_url`: ```java public class WireMockInitializer implements ApplicationContextInitializer<ConfigurableApplicationContext> {   @Override   public void initialize(ConfigurableApplicationContext configurableApplicationContext) {     WireMockServer wireMockServer = new WireMockServer(new WireMockConfiguration().dynamicPort());     wireMockServer.start();     configurableApplicationContext       .getBeanFactory()       .registerSingleton("wireMockServer", wireMockServer);     configurableApplicationContext.addApplicationListener(applicationEvent -> {       if (applicationEvent instanceof ContextClosedEvent) {         wireMockServer.stop();       }     });     TestPropertyValues       .of(Map.of("todo_base_url", "http://localhost:" + wireMockServer.port()))       .applyTo(configurableApplicationContext);   } } ``` Using an initializer, we bind the lifecycle of our WireMock server to a specific Spring context. **Using a dynamic port, we avoid port clashes when spawning multiple Spring contexts throughout our test suite. *We'll share the same WireMock server for all tests that [reuse this Spring context](https://rieckpil.de/improve-build-times-with-context-caching-from-spring-test/)***. We activate this initializer using `@ContextConfiguration` on top of our test class: ```java @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) @ContextConfiguration(initializers = {WireMockInitializer.class}) class TodoControllerIT {   @Autowired   private WireMockServer wireMockServer; } ``` *** **References**: - [Spring Boot Integration Tests With WireMock and JUnit 5](https://rieckpil.de/spring-boot-integration-tests-with-wiremock-and-junit-5/)