4 Replies Latest reply: Feb 1, 2010 4:13 AM by 807580 RSS

    Help Needed - JUnit test cases

    807580
      Hi,

      I am new to JUnit and I need a small help for writing JUnit test cases.

      I have two methods -
      public void setConfiguration(final Dictionary properties,final String service) {}
      
      public Dictionary getConfiguration(final String service, final String id) {}
      The setConfiguration method is used to store the properties into a text file.
      The getConfiguration method is used to get the properties from the text file.

      Can someone please suggest the approach to be taken for testing these, if you can provide a prototype it will be a great help to me.

      Regards,
      Sameer
        • 1. Re: Help Needed - JUnit test cases
          807580
          I am new to JUnit and I need a small help for writing JUnit test cases
          http://www.javabeat.net/tips/159-junit-40-example.html
          http://www.jguru.com/faq/view.jsp?EID=25537
          • 2. Re: Help Needed - JUnit test cases
            r035198x
            SAMEERRITU wrote:
            Hi,

            I am new to JUnit and I need a small help for writing JUnit test cases.

            I have two methods -
            public void setConfiguration(final Dictionary properties,final String service) {}
            
            public Dictionary getConfiguration(final String service, final String id) {}
            The setConfiguration method is used to store the properties into a text file.
            The getConfiguration method is used to get the properties from the text file.

            Can someone please suggest the approach to be taken for testing these, if you can provide a prototype it will be a great help to me.

            Regards,
            Sameer
            An approach is to integrate it with jmock so you can mock out certain parts of the system by setting up dummy return values.
            A text file does not present too many testing problems though. You could even just create one test one and use an actual file for the tests. You could put it on the class path and load it as a resource.
            • 3. Re: Help Needed - JUnit test cases
              jduprez
              If the loader/saver class enables to specify the file's path, it's easy to point it to a location you can control.
              Tests on the "set" method could then, starting from an empty file, exercise the set method and check afterwards that the file contains the expected content.
              Tests on the "get" method could, starting from a filled file, exercise the get method and check the created Dictionary contains the expected entries.

              Use the setup/cleanup methods (or whatever you call them, those annotated respectively with @Before, @After) to ensure the tests start from a clean and known state.
              I wouldn't play with resources on the classpath, given that
              1) you seem to be a beginner
              2) a classloader could cache the resource across tests
              3) I'm not sure it is defined whether you can write on a classpath resource, I can't even find a way from the standard API to write over it. That along with caching could sure lead to unexpected results.

              Feel free to come back for more specific questions if the replies so far still leave you mixed up.
              Regards,
              J.
              • 4. Re: Help Needed - JUnit test cases
                807580
                File.createTempFile() may come in handy here