Skip to content

Commit ec2c501

Browse files
committed
Improve documentation of end-to-end testing
Resolves #1338
1 parent 6cabff3 commit ec2c501

3 files changed

Lines changed: 160 additions & 1 deletion

File tree

spring-shell-docs/modules/ROOT/pages/testing.adoc

Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,4 +141,63 @@ public class ShellApplicationEndToEndTests {
141141
}
142142
----
143143

144-
This example demonstrates how to run a Spring Shell application in a test context and verify the output of a command using Spring Boot's `OutputCaptureExtension`.
144+
This example demonstrates how to run a Spring Shell application in a test context and verify the output of a command using Spring Boot's `OutputCaptureExtension`.
145+
146+
The caveat with this approach is that it is not convenient for testing multiple commands in the same test class due to the fact that the command is passed through the `args` property of the `@SpringBootTest` annotation. For testing multiple commands, it is recommended to use the Spring Shell testing utilities as described in the previous sections. Here is an example of how to test multiple commands using the Spring Shell testing utilities:
147+
148+
[source,java]
149+
----
150+
@SpringBootApplication
151+
public class GreetingShellApplication {
152+
153+
public static void main(String[] args) {
154+
SpringApplication.run(GreetingShellApplication.class, args);
155+
}
156+
157+
@Command
158+
public void hi(CommandContext context) {
159+
context.outputWriter().println("Hello world!");
160+
}
161+
162+
@Command
163+
public void bye(CommandContext context) {
164+
context.outputWriter().println("Goodbye world!");
165+
}
166+
167+
}
168+
169+
@ShellTest
170+
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.NONE,
171+
useMainMethod = SpringBootTest.UseMainMethod.ALWAYS, classes = { GreetingShellApplication.class },
172+
properties = { "spring.shell.interactive.enabled=false" })
173+
public class ShellApplicationEndToEndMultipleCommandsTests {
174+
175+
@Test
176+
void testCommandExecution(@Autowired ShellTestClient client) throws Exception {
177+
// when
178+
ShellScreen shellScreen = client.sendCommand("help");
179+
180+
// then
181+
ShellAssertions.assertThat(shellScreen).containsText("AVAILABLE COMMANDS");
182+
}
183+
184+
@Test
185+
void testHiCommandExecution(@Autowired ShellTestClient client) throws Exception {
186+
// when
187+
ShellScreen shellScreen = client.sendCommand("hi");
188+
189+
// then
190+
ShellAssertions.assertThat(shellScreen).containsText("Hello world!");
191+
}
192+
193+
@Test
194+
void testByeCommandExecution(@Autowired ShellTestClient client) throws Exception {
195+
// when
196+
ShellScreen shellScreen = client.sendCommand("bye");
197+
198+
// then
199+
ShellAssertions.assertThat(shellScreen).containsText("Goodbye world!");
200+
}
201+
202+
}
203+
----
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* Copyright 2025-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.test.e2e;
17+
18+
import org.springframework.boot.SpringApplication;
19+
import org.springframework.boot.autoconfigure.SpringBootApplication;
20+
import org.springframework.shell.core.command.CommandContext;
21+
import org.springframework.shell.core.command.annotation.Command;
22+
23+
@SpringBootApplication
24+
public class GreetingShellApplication {
25+
26+
public static void main(String[] args) {
27+
SpringApplication.run(GreetingShellApplication.class, args);
28+
}
29+
30+
@Command
31+
public void hi(CommandContext context) {
32+
context.outputWriter().println("Hello world!");
33+
}
34+
35+
@Command
36+
public void bye(CommandContext context) {
37+
context.outputWriter().println("Goodbye world!");
38+
}
39+
40+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
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.test.e2e;
17+
18+
import org.junit.jupiter.api.Test;
19+
20+
import org.springframework.beans.factory.annotation.Autowired;
21+
import org.springframework.boot.test.context.SpringBootTest;
22+
import org.springframework.shell.test.ShellAssertions;
23+
import org.springframework.shell.test.ShellScreen;
24+
import org.springframework.shell.test.ShellTestClient;
25+
import org.springframework.shell.test.autoconfigure.ShellTest;
26+
27+
@ShellTest
28+
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.NONE,
29+
useMainMethod = SpringBootTest.UseMainMethod.ALWAYS, classes = { GreetingShellApplication.class },
30+
properties = { "spring.shell.interactive.enabled=false" })
31+
public class ShellApplicationEndToEndMultipleCommandsTests {
32+
33+
@Test
34+
void testCommandExecution(@Autowired ShellTestClient client) throws Exception {
35+
// when
36+
ShellScreen shellScreen = client.sendCommand("help");
37+
38+
// then
39+
ShellAssertions.assertThat(shellScreen).containsText("AVAILABLE COMMANDS");
40+
}
41+
42+
@Test
43+
void testHiCommandExecution(@Autowired ShellTestClient client) throws Exception {
44+
// when
45+
ShellScreen shellScreen = client.sendCommand("hi");
46+
47+
// then
48+
ShellAssertions.assertThat(shellScreen).containsText("Hello world!");
49+
}
50+
51+
@Test
52+
void testByeCommandExecution(@Autowired ShellTestClient client) throws Exception {
53+
// when
54+
ShellScreen shellScreen = client.sendCommand("bye");
55+
56+
// then
57+
ShellAssertions.assertThat(shellScreen).containsText("Goodbye world!");
58+
}
59+
60+
}

0 commit comments

Comments
 (0)