简体   繁体   中英

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

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM