|
| 1 | +/* |
| 2 | + * Copyright 2026-present the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package org.springframework.shell.core.autoconfigure; |
| 17 | + |
| 18 | +import org.junit.jupiter.api.Test; |
| 19 | + |
| 20 | +import org.springframework.boot.ApplicationArguments; |
| 21 | +import org.springframework.boot.ApplicationRunner; |
| 22 | +import org.springframework.boot.autoconfigure.AutoConfigurations; |
| 23 | +import org.springframework.boot.autoconfigure.SpringBootApplication; |
| 24 | +import org.springframework.boot.test.context.runner.ApplicationContextRunner; |
| 25 | +import org.springframework.shell.core.command.annotation.Command; |
| 26 | +import org.springframework.stereotype.Component; |
| 27 | + |
| 28 | +import static org.junit.jupiter.api.Assertions.assertNotNull; |
| 29 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 30 | + |
| 31 | +/** |
| 32 | + * Tests for {@link ShellRunnerAutoConfiguration}. |
| 33 | + * |
| 34 | + * @author David Pilar |
| 35 | + */ |
| 36 | +class ShellRunnerAutoConfigurationTests { |
| 37 | + |
| 38 | + private final ApplicationContextRunner contextRunner = new ApplicationContextRunner() |
| 39 | + .withConfiguration(AutoConfigurations.of(SpringShellAutoConfiguration.class)) |
| 40 | + .withPropertyValues("spring.shell.interactive.enabled=false"); |
| 41 | + |
| 42 | + @Test |
| 43 | + void springShellApplicationRunnerShouldBeRegistered() { |
| 44 | + this.contextRunner.withUserConfiguration(TestShellApplication.class).run(context -> { |
| 45 | + ApplicationRunner shellRunner = context.getBean("springShellApplicationRunner", ApplicationRunner.class); |
| 46 | + assertNotNull(shellRunner, "springShellApplicationRunner should be registered"); |
| 47 | + }); |
| 48 | + } |
| 49 | + |
| 50 | + @Test |
| 51 | + void springShellApplicationRunnerShouldBeRegisteredAlongsideUserApplicationRunner() { |
| 52 | + this.contextRunner.withUserConfiguration(TestShellApplicationWithRunner.class).run(context -> { |
| 53 | + ApplicationRunner shellRunner = context.getBean("springShellApplicationRunner", ApplicationRunner.class); |
| 54 | + assertNotNull(shellRunner, |
| 55 | + "springShellApplicationRunner should be registered even when another ApplicationRunner exists"); |
| 56 | + |
| 57 | + assertTrue(context.containsBean("demoRunner"), "User-defined ApplicationRunner should also be registered"); |
| 58 | + }); |
| 59 | + } |
| 60 | + |
| 61 | + @SpringBootApplication |
| 62 | + static class TestShellApplication { |
| 63 | + |
| 64 | + @Command |
| 65 | + public void hi() { |
| 66 | + System.out.println("Hello!"); |
| 67 | + } |
| 68 | + |
| 69 | + } |
| 70 | + |
| 71 | + @SpringBootApplication |
| 72 | + static class TestShellApplicationWithRunner { |
| 73 | + |
| 74 | + @Command |
| 75 | + public void hi() { |
| 76 | + System.out.println("Hello!"); |
| 77 | + } |
| 78 | + |
| 79 | + @Component("demoRunner") |
| 80 | + static class DemoRunner implements ApplicationRunner { |
| 81 | + |
| 82 | + @Override |
| 83 | + public void run(ApplicationArguments args) { |
| 84 | + System.out.println("DemoRunner executed!"); |
| 85 | + } |
| 86 | + |
| 87 | + } |
| 88 | + |
| 89 | + } |
| 90 | + |
| 91 | +} |
0 commit comments