diff --git a/spring-boot-modules/spring-boot-testing/README.md b/spring-boot-modules/spring-boot-testing/README.md index 882e2be766..9bbaf337d7 100644 --- a/spring-boot-modules/spring-boot-testing/README.md +++ b/spring-boot-modules/spring-boot-testing/README.md @@ -13,3 +13,4 @@ The "REST With Spring" Classes: http://bit.ly/restwithspring - [Setting the Log Level in Spring Boot when Testing](https://www.baeldung.com/spring-boot-testing-log-level) - [Embedded Redis Server with Spring Boot Test](https://www.baeldung.com/spring-embedded-redis) - [Testing Spring Boot @ConfigurationProperties](https://www.baeldung.com/spring-boot-testing-configurationproperties) +- [Prevent ApplicationRunner or CommandLineRunner Beans From Executing During Junit Testing]() diff --git a/spring-boot-modules/spring-boot-testing/src/main/java/com/baeldung/prevent/commandline/application/runner/execution/ApplicationCommandLineRunnerApp.java b/spring-boot-modules/spring-boot-testing/src/main/java/com/baeldung/prevent/commandline/application/runner/execution/ApplicationCommandLineRunnerApp.java new file mode 100644 index 0000000000..76e18dfd2f --- /dev/null +++ b/spring-boot-modules/spring-boot-testing/src/main/java/com/baeldung/prevent/commandline/application/runner/execution/ApplicationCommandLineRunnerApp.java @@ -0,0 +1,11 @@ +package com.baeldung.prevent.commandline.application.runner.execution; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class ApplicationCommandLineRunnerApp { + public static void main(String[] args) { + SpringApplication.run(ApplicationCommandLineRunnerApp.class, args); + } +} diff --git a/spring-boot-modules/spring-boot-testing/src/main/java/com/baeldung/prevent/commandline/application/runner/execution/ApplicationRunnerTaskExecutor.java b/spring-boot-modules/spring-boot-testing/src/main/java/com/baeldung/prevent/commandline/application/runner/execution/ApplicationRunnerTaskExecutor.java new file mode 100644 index 0000000000..3bf08491bf --- /dev/null +++ b/spring-boot-modules/spring-boot-testing/src/main/java/com/baeldung/prevent/commandline/application/runner/execution/ApplicationRunnerTaskExecutor.java @@ -0,0 +1,27 @@ +package com.baeldung.prevent.commandline.application.runner.execution; + +import org.springframework.boot.ApplicationArguments; +import org.springframework.boot.ApplicationRunner; +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; +import org.springframework.context.annotation.Profile; +import org.springframework.stereotype.Component; + +@Profile("!test") +@ConditionalOnProperty( + prefix = "application.runner", + value = "enabled", + havingValue = "true", + matchIfMissing = true) +@Component +public class ApplicationRunnerTaskExecutor implements ApplicationRunner { + private TaskService taskService; + + public ApplicationRunnerTaskExecutor(TaskService taskService) { + this.taskService = taskService; + } + + @Override + public void run(ApplicationArguments args) throws Exception { + taskService.execute("application runner task"); + } +} diff --git a/spring-boot-modules/spring-boot-testing/src/main/java/com/baeldung/prevent/commandline/application/runner/execution/CommandLineTaskExecutor.java b/spring-boot-modules/spring-boot-testing/src/main/java/com/baeldung/prevent/commandline/application/runner/execution/CommandLineTaskExecutor.java new file mode 100644 index 0000000000..38fd3b9c0a --- /dev/null +++ b/spring-boot-modules/spring-boot-testing/src/main/java/com/baeldung/prevent/commandline/application/runner/execution/CommandLineTaskExecutor.java @@ -0,0 +1,26 @@ +package com.baeldung.prevent.commandline.application.runner.execution; + +import org.springframework.boot.CommandLineRunner; +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; +import org.springframework.context.annotation.Profile; +import org.springframework.stereotype.Component; + +@Profile("!test") +@ConditionalOnProperty( + prefix = "command.line.runner", + value = "enabled", + havingValue = "true", + matchIfMissing = true) +@Component +public class CommandLineTaskExecutor implements CommandLineRunner { + private TaskService taskService; + + public CommandLineTaskExecutor(TaskService taskService) { + this.taskService = taskService; + } + + @Override + public void run(String... args) throws Exception { + taskService.execute("command line runner task"); + } +} diff --git a/spring-boot-modules/spring-boot-testing/src/main/java/com/baeldung/prevent/commandline/application/runner/execution/TaskService.java b/spring-boot-modules/spring-boot-testing/src/main/java/com/baeldung/prevent/commandline/application/runner/execution/TaskService.java new file mode 100644 index 0000000000..dac437e72d --- /dev/null +++ b/spring-boot-modules/spring-boot-testing/src/main/java/com/baeldung/prevent/commandline/application/runner/execution/TaskService.java @@ -0,0 +1,14 @@ +package com.baeldung.prevent.commandline.application.runner.execution; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.stereotype.Service; + +@Service +public class TaskService { + private static Logger logger = LoggerFactory.getLogger(TaskService.class); + + public void execute(String task) { + logger.info("do " + task); + } +} diff --git a/spring-boot-modules/spring-boot-testing/src/test/java/com/baeldung/prevent/commandline/application/runner/execution/LoadSpringContextIntegrationTest.java b/spring-boot-modules/spring-boot-testing/src/test/java/com/baeldung/prevent/commandline/application/runner/execution/LoadSpringContextIntegrationTest.java new file mode 100644 index 0000000000..6698094550 --- /dev/null +++ b/spring-boot-modules/spring-boot-testing/src/test/java/com/baeldung/prevent/commandline/application/runner/execution/LoadSpringContextIntegrationTest.java @@ -0,0 +1,43 @@ +package com.baeldung.prevent.commandline.application.runner.execution; + +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.springframework.boot.ApplicationRunner; +import org.springframework.boot.CommandLineRunner; +import org.springframework.boot.test.context.ConfigFileApplicationContextInitializer; +import org.springframework.boot.test.mock.mockito.SpyBean; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit.jupiter.SpringExtension; + +import com.baeldung.prevent.commandline.application.runner.execution.ApplicationCommandLineRunnerApp; +import com.baeldung.prevent.commandline.application.runner.execution.TaskService; + +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; + +@ExtendWith(SpringExtension.class) +@ContextConfiguration(classes = { ApplicationCommandLineRunnerApp.class }, + initializers = ConfigFileApplicationContextInitializer.class) +public class LoadSpringContextIntegrationTest { + @SpyBean + TaskService taskService; + + @SpyBean + CommandLineRunner commandLineRunner; + + @SpyBean + ApplicationRunner applicationRunner; + + @Test + void whenContextLoads_thenRunnersDoNotRun() throws Exception { + assertNotNull(taskService); + assertNotNull(commandLineRunner); + assertNotNull(applicationRunner); + + verify(taskService, times(0)).execute(any()); + verify(commandLineRunner, times(0)).run(any()); + verify(applicationRunner, times(0)).run(any()); + } +} diff --git a/spring-boot-modules/spring-boot-testing/src/test/java/com/baeldung/prevent/commandline/application/runner/execution/RunApplicationIntegrationTest.java b/spring-boot-modules/spring-boot-testing/src/test/java/com/baeldung/prevent/commandline/application/runner/execution/RunApplicationIntegrationTest.java new file mode 100644 index 0000000000..26a7339f1d --- /dev/null +++ b/spring-boot-modules/spring-boot-testing/src/test/java/com/baeldung/prevent/commandline/application/runner/execution/RunApplicationIntegrationTest.java @@ -0,0 +1,26 @@ +package com.baeldung.prevent.commandline.application.runner.execution; + +import org.junit.jupiter.api.Test; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.test.mock.mockito.SpyBean; + +import com.baeldung.prevent.commandline.application.runner.execution.ApplicationRunnerTaskExecutor; +import com.baeldung.prevent.commandline.application.runner.execution.CommandLineTaskExecutor; + +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; + +@SpringBootTest +class RunApplicationIntegrationTest { + @SpyBean + ApplicationRunnerTaskExecutor applicationRunnerTaskExecutor; + @SpyBean + CommandLineTaskExecutor commandLineTaskExecutor; + + @Test + void whenContextLoads_thenRunnersRun() throws Exception { + verify(applicationRunnerTaskExecutor, times(1)).run(any()); + verify(commandLineTaskExecutor, times(1)).run(any()); + } +} diff --git a/spring-boot-modules/spring-boot-testing/src/test/java/com/baeldung/prevent/commandline/application/runner/execution/RunApplicationWithTestProfileIntegrationTest.java b/spring-boot-modules/spring-boot-testing/src/test/java/com/baeldung/prevent/commandline/application/runner/execution/RunApplicationWithTestProfileIntegrationTest.java new file mode 100644 index 0000000000..333cd2ab91 --- /dev/null +++ b/spring-boot-modules/spring-boot-testing/src/test/java/com/baeldung/prevent/commandline/application/runner/execution/RunApplicationWithTestProfileIntegrationTest.java @@ -0,0 +1,33 @@ +package com.baeldung.prevent.commandline.application.runner.execution; + +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.NoSuchBeanDefinitionException; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.context.ApplicationContext; +import org.springframework.test.context.ActiveProfiles; + +import com.baeldung.prevent.commandline.application.runner.execution.ApplicationRunnerTaskExecutor; +import com.baeldung.prevent.commandline.application.runner.execution.CommandLineTaskExecutor; +import com.baeldung.prevent.commandline.application.runner.execution.TaskService; + +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertThrows; + +@ActiveProfiles("test") +@SpringBootTest +class RunApplicationWithTestProfileIntegrationTest { + @Autowired + private ApplicationContext context; + + @Test + void whenContextLoads_thenRunnersAreNotLoaded() { + assertNotNull(context.getBean(TaskService.class)); + assertThrows(NoSuchBeanDefinitionException.class, + () -> context.getBean(CommandLineTaskExecutor.class), + "CommandLineRunner should not be loaded during this integration test"); + assertThrows(NoSuchBeanDefinitionException.class, + () -> context.getBean(ApplicationRunnerTaskExecutor.class), + "ApplicationRunner should not be loaded during this integration test"); + } +} diff --git a/spring-boot-modules/spring-boot-testing/src/test/java/com/baeldung/prevent/commandline/application/runner/execution/RunApplicationWithTestPropertiesIntegrationTest.java b/spring-boot-modules/spring-boot-testing/src/test/java/com/baeldung/prevent/commandline/application/runner/execution/RunApplicationWithTestPropertiesIntegrationTest.java new file mode 100644 index 0000000000..264a06a41e --- /dev/null +++ b/spring-boot-modules/spring-boot-testing/src/test/java/com/baeldung/prevent/commandline/application/runner/execution/RunApplicationWithTestPropertiesIntegrationTest.java @@ -0,0 +1,32 @@ +package com.baeldung.prevent.commandline.application.runner.execution; + +import static org.junit.jupiter.api.Assertions.*; + +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.NoSuchBeanDefinitionException; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.context.ApplicationContext; + +import com.baeldung.prevent.commandline.application.runner.execution.ApplicationRunnerTaskExecutor; +import com.baeldung.prevent.commandline.application.runner.execution.CommandLineTaskExecutor; +import com.baeldung.prevent.commandline.application.runner.execution.TaskService; + +@SpringBootTest(properties = { + "command.line.runner.enabled=false", + "application.runner.enabled=false" }) +class RunApplicationWithTestPropertiesIntegrationTest { + @Autowired + private ApplicationContext context; + + @Test + void whenContextLoads_thenRunnersAreNotLoaded() { + assertNotNull(context.getBean(TaskService.class)); + assertThrows(NoSuchBeanDefinitionException.class, + () -> context.getBean(CommandLineTaskExecutor.class), + "CommandLineRunner should not be loaded during this integration test"); + assertThrows(NoSuchBeanDefinitionException.class, + () -> context.getBean(ApplicationRunnerTaskExecutor.class), + "ApplicationRunner should not be loaded during this integration test"); + } +}