How can I test command line applications using maven?

I am working on a complex multi-module maven project. One of the modules is an executable jar that implements a command line application. I need to integrate testing this application. I need to run it several times, with several different commands, and check the exit status and stdout / err. However, I cannot find a plugin for maven that claims to support this, and also cannot track the JUnit library, which supports command line application testing.

Before you say, β€œDon't test the main method - do bla instead,” in which case I really want to test the main method, not some helper functions. The thing is to launch the application as a user in his own virtual machine and in the environment, and also check that he behaves himself - the correct definition of command line parameters, exit from the recording state and hot loading of the correct classes from the right bank plug-in .

+3
source share
2 answers

My current hack is to use apache-exec from the junit test method. It seems to work, but it is rather difficult to configure.

public void testCommandlineApp()
        throws IOException
{
    CommandLine cl = new CommandLine(resolveScriptNameForOS("./runme")); // add .sh/.bat
    cl.addArgument("inputFile.xml");

    exec.setWorkingDirectory(workingDir);
    exec.setExitValues(new int[] { 0, 1, 2 });

    int exitCode = exec.execute(cl);

    assertEquals("Exit code should be zero", 0, exitCode);
}
+1
source

script, maven-exec-plugin ?

STDOUT=$(mvn exec:java -DmainClass=yourMainClass --arg1 --arg2=value2)
RETURN_CODE=$?

# validate STDOUT
# validate RETURN_CODE

- shunit2, .

0

Source: https://habr.com/ru/post/1759361/


All Articles