How to check the value of an option in a unit test for Picocli

73 views
Skip to first unread message

Oliver Fischer

unread,
Sep 8, 2021, 2:15:49 PM9/8/21
to pic...@googlegroups.com
Dear all,

In a unit test for a utility using picocli, I would like to assert that picocli assigned the correct value to the option. How can I get the value associated with an option in a unit test?

Here is the current version of the unit test:

@Test
void callWithOptionForSuffix() {
    NextMajorSubcommand command = new NextMajorSubcommand();
    CommandLine cmdline = new CommandLine(command);

    ParseResult parseResult = cmdline.parseArgs("--suffix", "DELTA", "4.5.6");

    assertThat(parseResult.hasMatchedPositional(0)).isTrue();
    assertThat(parseResult.matchedOptions()).isNotEmpty();
    assertThat(parseResult.matchedOption("--suffix").isOption());
}

How can I do that?
-- 
N Oliver B. Fischer
A Schönhauser Allee 64, 10437 Berlin, Deutschland/Germany
P +49 30 44793251
M +49 178 7903538 
E o.b.f...@swe-blog.net 
S oliver.b.fischer
J oliver.b...@jabber.org
X http://xing.to/obf

Remko Popma

unread,
Sep 8, 2021, 5:24:09 PM9/8/21
to picocli
Hi Oliver,

Some ideas using the ParseResult:

assertEquals(parseResult.matchedOption("--suffix").getValue(), "DELTA");
or
assertEquals(parseResult.matchedOptionValue("--suffix", null), "DELTA");

alternatively you can query the  NextMajorSubcommand object (if it has visible getters):

assertEquals(command.getSuffix(), "DELTA");

Oliver Fischer

unread,
Sep 8, 2021, 6:10:23 PM9/8/21
to Remko Popma, picocli
Hi Remko,

Thank you for your hint to getValue() I figured out, that the correct assertion is this one:

assertThat(parseResult.matchedOption("--suffix").<Optional<String>>getValue()).hasValue("DELTA");

BTW: Picocli is awesome. I will use it also for the command line distribution of jQAssistant


Am 09.09.21 um 00:24 schrieb Remko Popma:
Hi Oliver,

Some ideas using the ParseResult:

assertEquals(parseResult.matchedOption("--suffix").getValue(), "DELTA");
or
assertEquals(parseResult.matchedOptionValue("--suffix", null), "DELTA");

alternatively you can query the  NextMajorSubcommand object (if it has visible getters):

assertEquals(command.getSuffix(), "DELTA");


Reply all
Reply to author
Forward
0 new messages