spring-projects / spring-shell

Spring based shell
http://projects.spring.io/spring-shell/
Apache License 2.0
710 stars 391 forks source link

Support testing full Spring Shell application #738

Open jvalkeal opened 1 year ago

jvalkeal commented 1 year ago

Relates to discussion in #721. @ShellTest was meant to test "slice" as defined in spring-boot which doesn't work when testing full spring-shell context. In boot @SpringBootTest exits but we can't use that as it then doesn't work with command line arguments that well. Also having @SpringBootTest enters interactive mode which makes tests to hung up.

This issue it to figure out if we need to modify @ShellTest to support full app(which might sound weird) or making @SpringShellBootTest which would be a shell specific annotation to provide same features as @ShellTest.

jvalkeal commented 3 months ago

If you do:

@Command
public class DemoCommands {

    @Command
    public String hi() {
        return "hello world";
    }
}
@SpringBootTest
@AutoConfigureShell
@AutoConfigureShellTestClient
class DemoApplicationTests {

    @Autowired
    ShellTestClient client;

    @Test
    void contextLoads() {
        NonInteractiveShellSession session = client.nonInterative("hi").run();

        await().atMost(2, TimeUnit.SECONDS).untilAsserted(() -> {
            List<String> lines = session.screen().lines();
            assertThat(lines).anySatisfy(line -> {
                assertThat(line).contains("hello world");
            });
        });
    }

}

This actually works and looks to be the way in boot docs how to bring in parts from "slice" test auto-configs.

It's mentioned i.e. in https://docs.spring.io/spring-boot/reference/testing/spring-boot-applications.html#testing.spring-boot-applications.spring-webflux-tests to bring in WebTestClient outside of "slice" testing with @SpringBootTest and we have a same case here.

It may be better to document this instead of coming up something shell specific.