Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Document how to test a picocli spring-boot application #812

Closed
remkop opened this issue Sep 13, 2019 · 5 comments
Closed

Document how to test a picocli spring-boot application #812

remkop opened this issue Sep 13, 2019 · 5 comments
Milestone

Comments

@remkop
Copy link
Owner

remkop commented Sep 13, 2019

Add the following dependency to your build.gradle:

dependencies {
    // ...
    testCompile "org.springframework.boot:spring-boot-starter-test:$springBootVersion"
}

A test can look like this:

package picocli.spring.boot.autoconfigure.sample.test;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import picocli.CommandLine;
import picocli.spring.boot.autoconfigure.sample.MyCommand;
import picocli.spring.boot.autoconfigure.sample.MySpringApp;

import static org.junit.Assert.*;
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.NONE;

@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = NONE, classes = MySpringApp.class)
public class ExampleTest {

    @Autowired
    CommandLine.IFactory factory;

    @Autowired
    MyCommand myCommand;

    @Test
    public void testParsingCommandLineArgs() {
        CommandLine.ParseResult parseResult = new CommandLine(myCommand, factory)
                .parseArgs("-x", "abc", "sub", "-y", "123");
        assertEquals("abc", myCommand.x);
        assertNull(myCommand.positionals);

        assertTrue(parseResult.hasSubcommand());
        CommandLine.ParseResult subResult = parseResult.subcommand();
        MyCommand.Sub sub = (MyCommand.Sub) subResult.commandSpec().userObject();
        assertEquals("123", sub.y);
        assertNull(sub.positionals);
    }

    @Test
    public void testUsageHelp() {
        String expected = String.format("" +
                "Usage: mycommand [-hV] [-x=<x>] [<positionals>...] [COMMAND]%n" +
                "      [<positionals>...]   positional params%n" +
                "  -h, --help               Show this help message and exit.%n" +
                "  -V, --version            Print version information and exit.%n" +
                "  -x=<x>                   optional option%n" +
                "Commands:%n" +
                "  sub%n");
        String actual = new CommandLine(myCommand, factory).getUsageMessage(CommandLine.Help.Ansi.OFF);
        assertEquals(expected, actual);
    }
}
@remkop remkop added this to the 4.0.5 milestone Sep 13, 2019
@dkirrane
Copy link

dkirrane commented Nov 1, 2019

Hi @remkop, I've tried this out but the test never runs? It just always prints usage details. Never gets to first line of test even. Looks like MySpringApp loads up and then exists with usage details.

@dkirrane
Copy link

dkirrane commented Nov 1, 2019

Nevermind I see the issue. I had this in the @SpringBootApplication run method

       if (args.length == 0) {
            new CommandLine(mainCommand, factory).usage(System.out);
            System.exit(2);
        }

@remkop
Copy link
Owner Author

remkop commented Nov 1, 2019

@dkirrane Can you show some more detail on the program output? Did you use the example above exactly, including the picocli.spring.boot.autoconfigure.sample.MyCommand and MySpringApp from the picocli-spring-boot-starter tests?

@remkop
Copy link
Owner Author

remkop commented Nov 1, 2019

Ah, our messages got crossed. Glad you solved it!

@remkop remkop modified the milestones: 4.1, 4.2 Nov 22, 2019
@remkop remkop modified the milestones: 4.2, 4.3 Jan 29, 2020
@remkop remkop modified the milestones: 4.3, 4.4 Apr 14, 2020
@remkop remkop modified the milestones: 4.4, 4.5 May 21, 2020
@remkop remkop modified the milestones: 4.5.2, 4.6, 4.7 Oct 14, 2020
@remkop remkop closed this as completed in 9f6895b Feb 15, 2022
@remkop
Copy link
Owner Author

remkop commented Feb 15, 2022

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants