Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

  • Go to the File menu and select New Junit Test Case...
  • Create a stub test case called Test_Person. DrJava will create a stub class that looks like the following
    Code Block
    /** * A JUnit testcaseclasstest case class.
     * Every method starting with the word "test" will be called when running
     * the test with JUnit. */
    class Test_PersonextendsTestCasePerson extends TestCase {
      /** * A test method.
       * (Replace "X" with a name describing the test. You may write as
       * many "testSomething" methods inthisclassin this class as you wish, and each
       * one will be called when running JUnit over overthisclassthis class.)
       */
      void testX() {
      }
    }
    
    At this point, do not worry about what "extends TestCase" means in the above. DrJava is re-using the class TestCase from the JUnit framework in some clever way without you having to deal with all the details of how to use an existing class from some other files.

...

  • Change the name of the stub method testX() in the above to test_nMonthTillBD()and add code to make it look like the code below.
    Code Block
    class Test_PersonextendsTestCasePerson extends TestCase {
     void test_nMonthTillBD() {
       Person peter =newPerson new Person(9);// a person born in September.
       assertEquals("Calling nMonthTillBD(2).", 7, peter.nMonthTillBD(2));
       assertEquals("Calling nMonthTillBD(9).", 0, peter.nMonthTillBD(9));
       assertEquals("Calling nMonthTillBD(12).", 9, peter.nMonthTillBD(12));
     }
    }
    
    Note that in the code for test_nMonthTillBD(), we have decided that we only need to know the birth month of a person in order to compute the number of months till the next birth day. As a result, we instantiate a Person object by passing only the birth month: new Person(9).

...

Code Block
class Person {
   int _bMonth;

   /**
    * Computes the number of months till the next birthday.
    */
   int nMonthTillBD(intcurrentMonthint currentMonth) {
      return return00;// todo
   }
}

Step 4.

After you have cleaned up your code for Person as shown in the above, you should be able to compile Test_Person. With Test_Person open, click on the Test button in DrJava tool bar.

...

Code Block
   int nMonthTillBD(int currentMonth) {
       return _bMonth - currentMonth; // todo
   }

Compile all and test again.

...

Lecture notes on Object-Oriented Programming using Java by Dung X. Nguyen and Stephen B. Wong: http://cnx.org/content/col10213/latest
Here is another link for a more detailed discussion of JUnit testing uisng DrJava: http://cnx.rice.edu/content/m11707/latest/
The following link contains a more elaborate discussion on JUnit testting and a more involved example of testing: http://junit.sourceforge.net/doc/testinfected/testing.htm.

Access Permissions: (Please don't edit)

...

.

...