From f0abfee11cb17337a1d20acb1801c8fea5bea86c Mon Sep 17 00:00:00 2001 From: Maciej Walkowiak Date: Mon, 15 Jan 2024 12:43:09 +0100 Subject: [PATCH] Avoid potential ClassCastException when handling context closed event. (#24) --- .../wiremock/spring/WireMockContextCustomizer.java | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/wiremock-spring-boot/src/main/java/com/maciejwalkowiak/wiremock/spring/WireMockContextCustomizer.java b/wiremock-spring-boot/src/main/java/com/maciejwalkowiak/wiremock/spring/WireMockContextCustomizer.java index 1fc0c60..3f371fb 100644 --- a/wiremock-spring-boot/src/main/java/com/maciejwalkowiak/wiremock/spring/WireMockContextCustomizer.java +++ b/wiremock-spring-boot/src/main/java/com/maciejwalkowiak/wiremock/spring/WireMockContextCustomizer.java @@ -12,7 +12,6 @@ import org.slf4j.LoggerFactory; import org.springframework.boot.test.util.TestPropertyValues; -import org.springframework.context.ApplicationListener; import org.springframework.context.ConfigurableApplicationContext; import org.springframework.context.event.ContextClosedEvent; import org.springframework.core.env.Environment; @@ -82,9 +81,11 @@ private void resolveOrCreateWireMockServer(ConfigurableApplicationContext contex Store.INSTANCE.store(context, options.name(), newServer); // add shutdown hook - context.addApplicationListener((ApplicationListener) event -> { - LOGGER.info("Stopping WireMockServer with name '{}'", options.name()); - newServer.stop(); + context.addApplicationListener(event -> { + if (event instanceof ContextClosedEvent) { + LOGGER.info("Stopping WireMockServer with name '{}'", options.name()); + newServer.stop(); + } }); // configure Spring environment property