Search code examples
javacommand-line-interfacepicocli

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


Solution

  • 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.