The new Spring Shell docs don't seem to provide any examples of how to integration test CLI commands in a Spring Boot context. Any pointers or examples would be appreciated.
-
Were you successful in your integration testing of spring shell in spring boot environment? – Indra Basak Oct 18 '17 at 00:05
-
@indra Yes, see my update – codemonkey Oct 29 '17 at 17:33
3 Answers
The method Shell#evaluate()
has been made public and has its very specific responsibility (evaluate just one command) for exactly that purpose. Please create an issue with the project if you feel like we should provide more (A documentation chapter about testing definitely needs to be written)

- 1,997
- 2
- 12
- 14
-
-
See this link for potentially more info: https://github.com/spring-projects/spring-shell/issues/171 – Joe Oct 29 '17 at 21:53
-
In 2.0.0, you also must disable the interactive shell, which is controlled by the "spring.shell.interactive.enabled" property. You can set it's value using any suitable method (profiles, properties in @SpringBootTest, etc) – Philippe Sevestre Sep 17 '19 at 05:02
Here is how I got this working.
You first need to override the default shell application runner to avoid getting stuck in the jline loop. You can do this by defining your own such as:
@Component
public class CliAppRunner implements ApplicationRunner {
public CliAppRunner() {
}
@Override
public void run(ApplicationArguments args) throws Exception {
//do nothing
}
}
Note that you will have to associate this custom Application runner against a "Test" profile so it overrides only during integration testing.
If you want to test a shell command "add 1 3", you then can write a test like this:
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes =CliConfig.class)
public class ShellCommandIntegrationTest {
@Autowired
private Shell shell;
@Test
public void runTest(){
Object result=shell.evaluate(new Input(){
@Override
public String rawText() {
return "add 1 3";
}
});
DefaultResultHandler resulthandler=new DefaultResultHandler();
resulthandler.handleResult(result);
}
}
Note that the above test does not Assert anything. You will probably have to write your own little implementation of the ResultHandler interface that deals with parsing/formatting of the result so that it can be asserted.
Hope it helps.

- 608
- 6
- 7
-
1I tried a couple of weeks back with a slightly different approach and it worked for me. [Here](https://github.com/indrabasak/spring-shell-example) is my example. – Indra Basak Oct 29 '17 at 18:22
Spring Shell 2.0.1 depends on Spring Boot 1.5.8, which in turn depends on Spring Framework 4.3.12. This makes researching how to implement tests challenging, since the latest version of Spring Shell does not depend on the latest versions of other Spring libraries. Take a look at my example project, sualeh/spring-shell-2-tests-example which has example unit, functional and integration tests for a sample Spring Shell application.

- 4,700
- 2
- 24
- 28