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 testcaseclass.
     * Every method starting with the word"test"will be called when running
     * the test with JUnit. */ 
    class Test_PersonextendsTestCase {
      /** * A test method.
       * (Replace"X"with a name describing the test. You may write as
       * many"testSomething"methods inthisclass as you wish, and each
       * one will be called when running JUnit overthisclass.)
       */
      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_PersonextendsTestCase {
     void test_nMonthTillBD() {
       Person peter =newPerson(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).

...