0

Here is my code for junit test case

public class HelptextValidation {

@Test
public void test() {
CLIReaderTest cli=new CLIReaderTest();
String Output="Test Execution";
assertEquals(cli.readCommandLineParameters(new String[]{"-h"}) , Output);


 }

 }

And is the class method for which test case is prepared

public class CLIReaderTest {
private String user = "";
private String password = "";
private String serverUrl = "";
private  boolean spit_everythingtoLog = false;
public boolean readCommandLineParameters(String[] args) {
Logger log = Logger.getLogger(CLIReader.class);
Options options = new Options();

Option helpOpt = Option.builder("h").longOpt("help").desc("Usage Help").build();
options.addOption(helpOpt);

Option serverurl = Option.builder("url").longOpt("server url").desc("Server url").required().hasArg().argName("url").build();
options.addOption(serverurl);

Option userOpt = Option.builder("u").longOpt("user").desc("User Name").hasArg().argName("user").required().build();
options.addOption(userOpt);

Option pwdOpt = Option.builder("p").longOpt("password").desc("user password").hasArg().argName("password").required().build();
options.addOption(pwdOpt);
  try {
  CommandLineParser parser = new DefaultParser();
  CommandLine cmd = parser.parse(options, args, true);
  
  if(cmd.hasOption("v")) {
    spit_everythingtoLog = true;

  }
  
  serverUrl = cmd.getOptionValue("url");
  user = cmd.getOptionValue("u");
  password = cmd.getOptionValue("p");
  streamName = cmd.getOptionValue("s");
  compList = cmd.getOptionValue("c");
  
  }

  catch (Exception e) {


   String temp1="--help";
   String temp2="[--help]";
   String temp3="[-h]";
   String temp4="-h";
  if(temp1.equals(args[0]) || temp2.equals(args[0]))
  {
    System.out.println("Test Execution");
    System.exit(1);
  }
  }
  

Here when user passes java -jar abc.jar -h in command line the output is "Test Execution" The same i want to do with my test case but i am unable to pass the cmd argument and compare it with string. Can anyone please help me out in this?

Christopher Moore
  • 15,626
  • 10
  • 42
  • 52
  • I think that this is really rather the job of an integration test rather than a unit test. If you want it as a unit test anyway, one thing you need to do is trap the `System.exit()` call so that your unit test doesn’t end there. It is possible. Does this answer your question? [Java: How to test methods that call System.exit()?](https://stackoverflow.com/questions/309396/java-how-to-test-methods-that-call-system-exit) – Ole V.V. Apr 01 '20 at 05:59
  • @Ole V.V. if i remove system.exit() then my code will go on executing the rest part of info message as well which i don't want to do it or maybe still i am getting what you are telling? – Rajat Krishnan Apr 01 '20 at 07:11

0 Answers0