1 Reply Latest reply: May 17, 2008 5:00 PM by 807601 RSS

    Junit Test for java program with main method only

    807601
      For my java program, the main method does everything because my teacher specified. Also, the program is invoked with command-line arguments with two options. Here is a snippet of my main method:
      if (args[0].equals("-all"))
      {
      //do something
      // count = 2;
      }
      else if (args[0].equals("-some))
      {
      //do something also
      // count = 3;
      }
      {code}
      So for my test case, I wrote three methods, one is the main, and two for each command-line option:
      {code}public void testOptionTwo()
       {
           //do something
           assertEquals(3, Myclass.count);
      
           }
      
      public void testOptionOne()
       {
           //do something too
          assertEquals(2, Myclass.count);
           }{code}
      
      but it turns out the to be assertion failure, for testOptionOne method, the actual for Myclass.count=5, which means, 2 was added to 3 from testOptionTwo. 
      Anyone know how can I fix that?                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    
        • 1. Re: Junit Test for java program with main method only
          807601
          JUnit is good and worth listening to if it tells you something about your code.
          for testOptionOne method, the actual for Myclass.count=5, which means, 2 was added to 3 from testOptionTwo.
          Anyone know how can I fix that?
          You have to find the code that gives Myclass.count the value of 5. It's none of the code you have posted.

          (It is possible that Myclass.count gets this value as a result of 2 being added, but that's not a necessary conclusion as you suggest.)