tags:

views:

97

answers:

3

I am doing some table testing in word, all of the JUnits are done but i am having trouble testing a method - as i am the tester in this project and not the coder i am struggling to understand what is actually correct or not

   public GregorianCalendar calcDeparture(String date, String time) {
        String[] calDate = new String[3];
        String[] calTime = new String[2];
        calDate[0] = (date.substring(0, 2)); //Dat
        calDate[1] = date.substring(2, 5); //Month
        calDate[2] = "20" + date.substring(5, 7); //Year
        calTime = time.split(":");

        //Adds the year, month and day and hour and minute from the above splited arrays
        int year = Integer.parseInt(calDate[2]);
        int month = monthToInt(calDate[1]);
        int day = Integer.parseInt(calDate[0]);
        int hour = Integer.parseInt(calTime[0]);
        int minute = Integer.parseInt(calTime[1]);

        GregorianCalendar newDeparture = new GregorianCalendar(year, month, day, hour, minute, 0);
        return newDeparture;
    }

This is the method I am testing. If i pass it the values of "01AUG07 "14:40" i get a gregorian calander back but i don't know if the values inside of it are correct so i can't tick the passed or failed box. What i get back in the BlueJ object inspector is a load of really long numbers :D

can i get some help please

thanks

+1  A: 

Why can you just not use the standard getters to check the individual fields, along the lines of:

Calendar cal = calcDeparture("01AUG07", "14:40");
if (cal.get(Calendar.YEAR) != 2007) { ... }
paxdiablo
+2  A: 
  1. BlueJ? Consider using an IDE, not an educational software
  2. The method is terribly written - working with dates using a strictly-formatted String is wrong.
  3. Calendar (which is the supertype of GregorianCalendar) has the get method, which you can use like:

    Calendar calendar = calcDeparture(yourDate, yourTime);
    int day = calendar.get(Calendar.DAY_OF_YEAR);
    int moth = calendar.get(Calendar.MONTH); //this is 0 based;
    

    and so on

Bozho
+3  A: 

I suggest to check all the relevant values of the calendar at the same time using a SimpleDateFormat() like so:

SimpleDateFormat f = new SimpleDateFormat ("yyyy-MM-dd HH:mm");
String s = f.format (calcDeparture(yourDate, yourTime));
assertEquals ("2007-08-01 14:40", s);

Now call your method with odd dates (like 31.12.2999, August 45th, February 29th 2001, etc) to see what you get and how you should handle errors.

Aaron Digulla
Nice and simple solution!
Fabian Steeg
@OP: And please inform the original developer about the SimpleDateFormat class and its usage. He/She might offer you a nice share in his/her this month's salary. If not, then you will surely be able to get a beer, at least.
Adeel Ansari
@OP: You share this test code with the original developer, and its highly likely this method will not exist anymore for you to test.
Adeel Ansari