java - junit test for apache CommandLineParser example -


here example code using apache commandlineparser

public class foo {    public static void main(final string[] args) throws exception {      options options = new options();      options.addoption("x",             true, "comment param x");      options.addoption("y",             true, "comment param y");       commandline commandline = null;      commandlineparser parser = new posixparser();      try {         commandline = parser.parse(options, args);     } catch (parseexception e) {         throw new runtimeexception("error parsing arguments!");     }      if (!commandline.hasoption("x")) {         throw new illegalargumentexception("x"                 + " option missing!");     }      string numberofcolumns = commandline.getoptionvalue("x");  :  :  } 

junit test code:

@test public void testfoo() throws exception {      args = new string[2];      args[0] = "x" + "=" + "hello";      args[1] = "y" + "=" + "world";      foo.main(args);  } 

my problem/question:

commandlineparser keeps complaining "x option missing!". believe, way passing parameter , values command line parser wrong. tried other ways well.

    args[0] = "-x" + "=" + "hello";     args[1] = "-y" + "=" + "world"; 

and also

    args[0] = "x"      args[1] = "hello";     args[2] = "y"      args[3] = "world"; 

can one, tell me correct format pass arguments , values succeed?

thanks in advance.

as far know arguments must start minus.

args[0] = "-x"; args[1] = "hello"; args[2] = "-y"; args[3] = "world"; 

Comments

Popular posts from this blog

java - WrongTypeOfReturnValue exception thrown when unit testing using mockito -

php - Magento - Deleted Base url key -

android - How to disable Button if EditText is empty ? -