Filippos Vlahos
Filippos Vlahos

Reputation: 127

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

Upvotes: 6

Views: 3863

Answers (1)

Remko Popma
Remko Popma

Reputation: 36754

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.

Upvotes: 11

Related Questions