[BAEL-3913] Prevent Application / CommandLineRunner classes from executing during JUnit testing (#8920)

* kkaravitis@gmail.com hexagonal architecture in java

* Removed evaluation article code

* Removed evaluation article code

* [BAEL-3913] Initial commit for prevent commandline application runner execution during JUnit test article

* [BAEL-3913] Fixed broken lines format and reduced the test method names length

* [BAEL-3913] Fixed broken lines format
This commit is contained in:
Konstantinos Karavitis
2020-03-26 19:57:09 +02:00
committed by GitHub
parent 1807233610
commit f03374a805
9 changed files with 213 additions and 0 deletions
@@ -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);
}
}
@@ -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");
}
}
@@ -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");
}
}
@@ -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);
}
}