codemonkey
codemonkey

Reputation: 608

How to Integration Test Spring Shell v2.x

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.

Upvotes: 4

Views: 4115

Answers (3)

Sualeh Fatehi
Sualeh Fatehi

Reputation: 4784

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.

Upvotes: 0

codemonkey
codemonkey

Reputation: 608

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.

Upvotes: 0

ebottard
ebottard

Reputation: 1997

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)

Upvotes: 2

Related Questions