diff --git a/spring-boot-project/spring-boot-testcontainers/src/main/java/org/springframework/boot/testcontainers/lifecycle/TestcontainersLifecycleApplicationContextInitializer.java b/spring-boot-project/spring-boot-testcontainers/src/main/java/org/springframework/boot/testcontainers/lifecycle/TestcontainersLifecycleApplicationContextInitializer.java index 944a61af70..7c768c71dc 100644 --- a/spring-boot-project/spring-boot-testcontainers/src/main/java/org/springframework/boot/testcontainers/lifecycle/TestcontainersLifecycleApplicationContextInitializer.java +++ b/spring-boot-project/spring-boot-testcontainers/src/main/java/org/springframework/boot/testcontainers/lifecycle/TestcontainersLifecycleApplicationContextInitializer.java @@ -23,7 +23,7 @@ import org.springframework.context.ConfigurableApplicationContext; /** * {@link ApplicationContextInitializer} to manage the lifecycle of {@link Startable - * startable container}. + * startable containers}. * * @author Phillip Webb * @since 3.1.0 diff --git a/spring-boot-project/spring-boot-testcontainers/src/main/java/org/springframework/boot/testcontainers/lifecycle/TestcontainersLifecycleBeanPostProcessor.java b/spring-boot-project/spring-boot-testcontainers/src/main/java/org/springframework/boot/testcontainers/lifecycle/TestcontainersLifecycleBeanPostProcessor.java index dab2d53b1c..710261aa06 100644 --- a/spring-boot-project/spring-boot-testcontainers/src/main/java/org/springframework/boot/testcontainers/lifecycle/TestcontainersLifecycleBeanPostProcessor.java +++ b/spring-boot-project/spring-boot-testcontainers/src/main/java/org/springframework/boot/testcontainers/lifecycle/TestcontainersLifecycleBeanPostProcessor.java @@ -20,16 +20,16 @@ import org.testcontainers.lifecycle.Startable; import org.springframework.beans.BeansException; import org.springframework.beans.factory.config.BeanPostProcessor; -import org.springframework.beans.factory.config.DestructionAwareBeanPostProcessor; /** * {@link BeanPostProcessor} to manage the lifecycle of {@link Startable startable - * container}. + * containers}. * * @author Phillip Webb + * @author Stephane Nicoll * @see TestcontainersLifecycleApplicationContextInitializer */ -class TestcontainersLifecycleBeanPostProcessor implements DestructionAwareBeanPostProcessor { +class TestcontainersLifecycleBeanPostProcessor implements BeanPostProcessor { @Override public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException { @@ -39,14 +39,4 @@ class TestcontainersLifecycleBeanPostProcessor implements DestructionAwareBeanPo return bean; } - @Override - public void postProcessBeforeDestruction(Object bean, String beanName) throws BeansException { - // Startable extends AutoCloseable will always be closed by context - } - - @Override - public boolean requiresDestruction(Object bean) { - return bean instanceof Startable; - } - } diff --git a/spring-boot-project/spring-boot-testcontainers/src/test/java/org/springframework/boot/testcontainers/lifecycle/TestcontainersLifecycleApplicationContextInitializerTests.java b/spring-boot-project/spring-boot-testcontainers/src/test/java/org/springframework/boot/testcontainers/lifecycle/TestcontainersLifecycleApplicationContextInitializerTests.java new file mode 100644 index 0000000000..589de361b3 --- /dev/null +++ b/spring-boot-project/spring-boot-testcontainers/src/test/java/org/springframework/boot/testcontainers/lifecycle/TestcontainersLifecycleApplicationContextInitializerTests.java @@ -0,0 +1,59 @@ +/* + * Copyright 2012-2023 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.boot.testcontainers.lifecycle; + +import org.junit.jupiter.api.Test; +import org.testcontainers.lifecycle.Startable; + +import org.springframework.context.annotation.AnnotationConfigApplicationContext; + +import static org.mockito.BDDMockito.then; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.never; + +/** + * Tests for {@link TestcontainersLifecycleApplicationContextInitializer}. + * + * @author Stephane Nicoll + */ +class TestcontainersLifecycleApplicationContextInitializerTests { + + @Test + void whenStartableBeanInvokesStartOnRefresh() { + Startable container = mock(Startable.class); + try (AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext()) { + applicationContext.registerBean("container", Startable.class, () -> container); + new TestcontainersLifecycleApplicationContextInitializer().initialize(applicationContext); + then(container).shouldHaveNoInteractions(); + applicationContext.refresh(); + then(container).should().start(); + } + } + + @Test + void whenStartableBeanInvokesDestroyOnShutdown() { + Startable mock = mock(Startable.class); + AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(); + applicationContext.registerBean("container", Startable.class, () -> mock); + new TestcontainersLifecycleApplicationContextInitializer().initialize(applicationContext); + applicationContext.refresh(); + then(mock).should(never()).close(); + applicationContext.close(); + then(mock).should().close(); + } + +}