Results 1 to 7 of 7
  1. #1
    joshft91 is offline Member
    Join Date
    Oct 2010
    Posts
    8
    Rep Power
    0

    Default Creating a test class

    Hey guys ... new here. I'm a freshman in college taking my first programming course, so bear with me as I ask some pretty simple questions.

    Basically what I'm doing with this class is keep track of a date that inputted. It should handle everything correctly ... However, that's not the piece of code I have a question with (I'm just putting it in for you guys to refer to if you need it).
    Java Code:
    package calendar;
    
    /**
     * A "calendar" that keeps track of the date 
     */
    
    public class Date {
      private int day;
      private String month;
      private int year;
        
      
      /**
       * Creates a instance of a date and starts it on November 2, 2010
       */
      
      public Date() {
        day = 2;
        month = "November";
        year = 2010;
      }
      
      /**
       * Creates a date where the user specifies the date to start on.
       * The month must contain a string with a valid month name.
       * @ensure The day must contain an integer with values between 1-30 or 1-31 depending on the date.
       * For example, if the month is January, the day must have a minimum value of 1 and a maximum value of 31.
       * Note that February and leap year are exceptions.
       * The year must be non negative.
       * @param m A string that represents the month passed in.
       * @param day An integer that represents the day passsed in
       * @param year Also an integer that represents the year passed in - must be positive.
       */
        
      public Date (String m, int day, int year) {
        if (m.equals("January") || m.equals("February") || m.equals("March") || m.equals("April") || m.equals("May") || m.equals("June") || m.equals("July") || m.equals("August") || m.equals("September") || m.equals("October")
              || m.equals("November") || m.equals("December")) {
        month = m;
        }
        
        this.day = day;
        
        if (year > 0) {
          this.year = year;
        }
      }
        
        
        
      /**
       * @ensure that the month, day, and year are all valid inputs.
       * @return Date returns the current date in the form of a string
       */
      
      
      public String getDate() {
                                                                                                                                                                  
        return month + " " + day + "," + " "  + year + ".";
      }
      /**
       * @return day the day of the current date.
       */
      
      public int getDay() {
        return day;
      }
      
      /**
       * @That the month is capitalized and spelled correctly.
       * @return month the month of the current date.  
       */ 
      
      public String getMonth() {
        return month;
      }
      
      /**
       * @ensure year > 0
       * @return year returns the year of the current date.  The year must be a non-negative number.
       */
      
      public int getYear() {
        return year;
      }
      
      /**
       * @require that the method performs the correct operations
       * This method will run the tomorrow() method 7 times, which 
       * will automatically correct what happens when the date gets 
       * to the end of the month.
       */
      
      public void nextWeek() {
        
        for (int i = 0; i <7; i++) {
          tomorrow();
        }
      }
          
      /**
       * This method assures that if the month is on a certain date and day, 
       * it will automatically kick down to the next correct month and day.
       * Leap year is correctly handled by a couple else if statements saying
       * that if the year divided by 4 it is a leap year.  However, that is not
       * the only exception - if the year divisible by 100 with a remainder of not
       * 0 and if the year is divisble by 400 with no remainder then the year is 
       * also a leap year.
       */
      
      public void tomorrow() {
        if (month.equals("January") && day == 31) {
          day = 1;
          month = "February";
        }
        else if (month.equals("February") && year%4 != 0 && day == 28) {
          day = 1;
          month = "March";
      }
        else if (month.equals("February") && year%4 == 0 &&(year%100 != 0 || year%400 == 0) && day == 28) {
          day = 29;
        }
        else if (month.equals("February") && day == 29) {
          day = 1;
          month = "March";
        }
        else if (month.equals("March") && day == 31) {
          day = 1;
          month = "April";
        }
        else if (month.equals("April") && day == 30) {
          day = 1;
          month = "May";
        }
        else if (month.equals("May") && day == 31) {
          day = 1;
          month = "June";
        }
        else if (month.equals("June") && day == 30) {
          day = 1;
          month = "July";
        }
        else if (month.equals("July") && day == 31) {
          day = 1;
          month = "August";
        }
        else if (month.equals("August") && day == 31) {
          day = 1;
          month = "September";
        }
        else if (month.equals("September") && day == 30) {
          day = 1;
          month = "October";
        }
        else if (month.equals("October") && day == 31) {
          day =1;
          month = "November";
        }
        else if (month.equals("November") && day == 31) {
          day = 1;
          month = "December";
        }
        else if (month.equals("December") && day == 31) {
        day = 1;
        month = "January";
        year = year + 1;
      }
      else {
        day = day + 1;
      }
      }
    }
    The next thing we were required to do is to create a test class to test everything ... This is what I have so far, however I'm not sure how to procede.

    Java Code:
    package calendar;
    
    import org.junit.*;
    import static org.junit.Assert.*;
    
    public class DateTest {
      private Date endJanuary; 
      private Date leapYear;
      private Date middleJuly;
      private Date endDecember;
      
      @Before
      public void setUp() {
        endJanuary = new Date("January",31,2010);
        leapYear = new Date("February",29,2012);
        middleJuly = new Date("July",17,2010);
        endDecember = new Date ("December",31,2010);
      }
      
      @Test
      public void testNextWeek() {
       // endJanuary.nextWeek();
       // leapYear.nextWeek();
        //middleJuly.nextWeek();
        //endDecember.nextWeek();
        assertEquals("February",7,2010);
        assertEquals("March",7,2012);
        assertEquals("July",24,2010);
        assertEquals("January",6,2011);
      }
    }
    After running the test I get this as an error message
    Java Code:
    1 test failed:
      calendar.DateTest
        testNextWeek
    File: D:\Data\Documents\WSU_Fall_2010\CS234\assignments\assignment8\calendar\DateTest.java  [line: 26]
    Failure: java.lang.AssertionError: February expected:<7> but was:<2010>
    If anyone could give me some insight as to what I need to be doing different, I would be much appreciated.

  2. #2
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    5

    Default

    Are you familiar with assertEquals()?

    Here is the code you are executing: Assert (JUnit API)

    So basically you're throwing an error in any case that 7 != 2010. And that's always the case.

  3. #3
    joshft91 is offline Member
    Join Date
    Oct 2010
    Posts
    8
    Rep Power
    0

    Default

    Thanks for the reply, Zack. I see what you're saying ... the example we had worked on in class somehow made sure that the method they were testing was functioning correctly.

    I really don't know a lot about assertEquals (...) because our teacher basically showed us an example in class real quick ... I didn't have a great understanding of how they worked.

    However, one of the requirements for this assignment is to create a test class that makes sure the nextWeek() and tomorrow() methods work. I decided to start with the nextWeek() method, but really didn't get anywhere - sorta just started throwing things together.

    I guess I'm sorta clueless as to how to proceed with this test class.

    Thanks again,
    joshft91

  4. #4
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    5

    Default

    Personally I would design the Date class differently.

    However, if I were given your Date class and told to do a test case, I would do something like this:
    Java Code:
    endJanuary.nextWeek();
    if (endJanuary.month.equals("February") && endJanuary.day == 7 && endJanuary.year == 2010) {
        // Print success
    }

    That should help you get thinking, I think. At least for your test case. Good luck!

  5. #5
    joshft91 is offline Member
    Join Date
    Oct 2010
    Posts
    8
    Rep Power
    0

    Default

    Alright, thanks - I see how that would work.
    I emailed my professor to see how they would like me to correctly write this test class... here's the response
    So if you wanted to check if a date was January 1, 2010, you would need three separate assert statements. The first would check the month by calling getMonth(), the second check the day with getDay() and the third getYear(), all on the same object.
    I think I understand what she's getting at, but could anyone show me a simple example of how I would use all 3 assert statements to correctly check that?

    Thanks again,
    joshft91

  6. #6
    joshft91 is offline Member
    Join Date
    Oct 2010
    Posts
    8
    Rep Power
    0

    Default

    Sorry for the bump, but I don't wanna see this get buried.

  7. #7
    Singing Boyo is offline Senior Member
    Join Date
    Mar 2009
    Posts
    552
    Rep Power
    6

    Default

    Pseudoishcode:

    Java Code:
    assume object d is a date;
    assertEquals(msg, d.getMonth(), expected value);
    assertEquals(msg, d.getDay(), expected);
    assertEquals(msg, d.getYear(), expected);
    Do that for each date you need to check. You could even put it in another method, (void assertDate(Date check, String month, int day, int year)) but that might be overkill.
    If the above doesn't make sense to you, ignore it, but remember it - might be useful!
    And if you just randomly taught yourself to program, well... you're just like me!

Similar Threads

  1. Help with this test class
    By fsuarjun03 in forum New To Java
    Replies: 10
    Last Post: 06-20-2011, 03:56 AM
  2. Replies: 2
    Last Post: 12-31-2009, 12:45 PM
  3. [SOLVED] Best practice for creating test classes
    By mwildam in forum New To Java
    Replies: 3
    Last Post: 08-27-2008, 04:59 PM
  4. Replies: 0
    Last Post: 06-23-2008, 11:16 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •