1. Testing
Testing cli application is difficult due to various reasons:
-
There are differences between OS’s.
-
Within OS there may be different shell implementations in use.
-
What goes into a shell and comes out from a shell my be totally different what you see in shell itself due to control characters.
-
Shell may feel syncronous but most likely it is not meaning when someting is written into it, you can’t assume next update in in it is not final.
| Testing support is currently under development and will be unstable for various parts. |
1.1. Basics
Spring Shell provides a number of utilities and annotations to help when testing your application.
Test support is provided by two modules: spring-shell-test contains core items, and
spring-shell-test-autoconfigure supports auto-configuration for tests.
To test interactive commands.
@ShellTest
@DirtiesContext(classMode = ClassMode.AFTER_EACH_TEST_METHOD)
class InteractiveTestSample {
@Autowired
ShellTestClient client;
@Test
void test() {
InteractiveShellSession session = client
.interactive()
.run();
await().atMost(2, TimeUnit.SECONDS).untilAsserted(() -> {
ShellAssertions.assertThat(session.screen())
.containsText("shell");
});
session.write(session.writeSequence().text("help").carriageReturn().build());
await().atMost(2, TimeUnit.SECONDS).untilAsserted(() -> {
ShellAssertions.assertThat(session.screen())
.containsText("AVAILABLE COMMANDS");
});
}
}
To test non-interactive commands.
@ShellTest
@DirtiesContext(classMode = ClassMode.AFTER_EACH_TEST_METHOD)
class NonInteractiveTestSample {
@Autowired
ShellTestClient client;
@Test
void test() {
NonInteractiveShellSession session = client
.nonInterative("help", "help")
.run();
await().atMost(2, TimeUnit.SECONDS).untilAsserted(() -> {
ShellAssertions.assertThat(session.screen())
.containsText("AVAILABLE COMMANDS");
});
}
}