Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Testing pico cli commands, Java

I've developed a Java API and I've built a command line interface to consume it using PicoCli

What is the proper way to test my pico commands?

Thanks in advance

like image 692
Filippos Vlahos Avatar asked Jul 25 '26 07:07

Filippos Vlahos


1 Answers

You can do black box testing by verifying the exit code and the output of the program to the standard output stream and standard error stream.

You can do white box testing by keeping a reference to your application and asserting on the state of your application after giving it various command line inputs.

For example:

MyApp app = new MyApp();
StringWriter sw = new StringWriter();
CommandLine cmd = new CommandLine(app);
cmd.setOut(new PrintWriter(sw));

// black box testing 
int exitCode = cmd.execute("-x", "-y=123");
assertEquals(0, exitCode);
assertEquals("Your output is abc...", sw.toString());

// white box testing 
assertEquals("expectedValue1", app.getState1());
assertEquals("expectedValue2", app.getState2());

Update: the picocli user manual now has a separate section about Testing.

like image 85
Remko Popma Avatar answered Jul 26 '26 19:07

Remko Popma



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!