2015-09-04 24 views
-1

下面是一個表示使用小時和分鐘的時間(秒未顯示)的類。使用OOP java進行簡單的Junit測試

public class ClassTime { 

    public static int hour; 
    public static int minute; 
    public static String amPm; 

    //Initializes the object using specified parameter values. 
    public ClassTime(int hour, int minute, String amPm){ 

     //hour value between 1 and 12, both inclusive. 
     boolean validHour = false; 
     if (hour >= 1 && hour <= 12){ 
      validHour = true; 
      this.hour = hour; 
     } else { 
      throw new IllegalArgumentException("Invalid hour value"); 
     } 

     //minute value between 0 and 59, both inclusive. 
     boolean validMinute = false; 
     if (minute >= 0 && minute <= 59){ 
      validMinute = true; 
      this.minute = minute; 
     } else { 
      throw new IllegalArgumentException("Invalid minutes value"); 
     } 

     //amPm is either "am" or "pm". 
     if (amPm.equalsIgnoreCase("am")){ 
      this.amPm = amPm; 
     } else if (amPm.equalsIgnoreCase("pm")){ 
      this.amPm = amPm; 
     } else { 
      throw new IllegalArgumentException("Invalid am/pm value"); 
     } 

    } 

    /* 
    * Returns a string using the format "hour:minutes am" or "hour:minutes pm". 
    * A single space is used in between minutes and am/pm. The minutes always 
    * appear with two digits (e.g., 0 minutes will be "00"). 
    */ 
    public String toString(){ 
     String toBeReturned = "hour:" + String.format("%02d", this.minute) + 
       " " + amPm; 
     return toBeReturned; 
    } 

    /* 
    * Compares two time objects. Two time objects are considered equal if 
    * they represent the same time. 
    */ 
    public boolean equals(ClassTime obj){ 

     boolean equal = false; 

     if (obj.minute == this.minute && obj.hour == this.hour && 
       obj.amPm.equalsIgnoreCase(this.amPm)){ 
      equal = true; 
     } 

     return equal; 
    } 

    /* 
    * Compares two time objects. Returns -1 if the current object is a time 
    * that precedes the time parameter, 0 if the current object and the time 
    * parameter represent the same time, and 1 if the current object represents 
    * a time that is after the time parameter. 
    */ 
    public int compareTo(ClassTime obj){ 

     int returnNum = 2; 

     if(this.amPm.equalsIgnoreCase("am") && obj.amPm.equalsIgnoreCase("pm")){ 
      returnNum = -1; 
     } else if (this.amPm.equalsIgnoreCase("pm") && 
       obj.amPm.equalsIgnoreCase("am")){ 
      returnNum = 1; 
     } else if (this.amPm.equalsIgnoreCase(obj.amPm)){ 
      if (this.hour < obj.hour){ 
       returnNum = -1; 
      } else if (this.hour > obj.hour){ 
       returnNum = 1; 
      } else if (this.hour == obj.hour){ 
       if (this.minute < obj.minute){ 
        returnNum = -1; 
       } else if (this.minute > obj.minute){ 
        returnNum = 1; 
       } else if (this.minute == obj.minute){ 
        returnNum = 0; 
       } 
      } 
     } 
     return returnNum; 
    } 

    /* 
    * Returns a new time object corresponding to the time we will have after 
    * increasing the time parameter by the specified number of minutes. 
    */ 
    public static ClassTime increaseByMinutes(ClassTime obj, int minutes){ 

     //create variables that monitor changes in minutes, hours, and amPm. 
     int minValue = obj.minute + minutes; 
     int hourValue = obj.hour; 
     String amPmValue = obj.amPm; 

     /* 
     * increments hour and minutes if the total minutes is below two hours, 
     * but greater tan or equal to 1 hour. 
     */ 
     if(minValue > 59 && minValue < 120){ 
      minValue = minValue % 60; 
      hourValue = hourValue + 1; 
      if (hourValue > 12){ 
       hourValue = hourValue % 12; 
      } else if(hourValue == 12 && amPmValue.equalsIgnoreCase("am")){ 
       amPmValue = "pm"; 
      } else if (hourValue == 12 && amPmValue.equalsIgnoreCase("pm")){ 
       amPmValue = "am"; 
      } 

      /* 
      * Increment for when the total amount of minutes is greater 
      * or equal to 2 hours. 
      */ 
     } else if (minValue > 119){ 
      for(int i = 0; i <= (minValue/60); i++){ 
       hourValue++; 
       if(hourValue > 12){ 
        hourValue = hourValue % 12; 
       } else if (hourValue == 12 && amPmValue.equalsIgnoreCase("am")){ 
        amPmValue = "pm"; 
       } else if (hourValue == 12 && amPmValue.equalsIgnoreCase("pm")){ 
        amPmValue = "am"; 
       } 
      } 
     } 

     /* 
     * Create a new ClassTime object with the found values of minutes, hours, 
     * and amPm Value. This is what will be returned. 
     */ 
     ClassTime newObject = new ClassTime(hour, minute, amPm); 

     newObject.minute = minValue; 
     newObject.hour = hourValue; 
     newObject.amPm = amPmValue; 

     return newObject; 

    } 


    public static void main(String[] args) { 

    } 

} 

我不知道如何在JUnit測試用例中測試構造函數或方法。到目前爲止,我能想出的構造函數的唯一的事情就是:

import static org.junit.Assert.*; 

import org.junit.Test; 

public class JunitTests { 

    @Test 
    public void testClassTime() { 
     ClassTime object1 = new ClassTime(3, 30, "pm"); 



    } 

} 

我將如何完成這樣的構造函數的測試,也許只是1的方法。請不要全部做。

+1

下一步是**斷言**的東西。閱讀JUnit文檔以獲取更多信息。 – Andreas

回答

1

我建議你閱讀單元測試,你的問題有點太寬泛。

基本上這個想法是,給出某些情況下,當一個的事情發生,然後一些條件應該是真實的。 (我個人添加評論我的測試,幫我想在這些方面。)

下面是一個抽象的例子:

  1. 鑑於麪包和烤麪包機
  2. 當我把麪包烤麪包機和獲得出來的時候,它的完成
  3. 那麼它應該是烤

查找JUnit的斷言庫,以幫助找到好方法,斷言條件。在查找JUnit示例時,您通常會看到類似assertTrue(...)assertNotNull(...)的東西,這些是來自Assert類的靜態方法,我提到這一點是因爲您看起來像一個新手,並且我不希望您被示例弄糊塗。通常所有這些方法都是在示例中靜態導入的。

你在你的測試用例有什麼是目前給出的,一個可以做toString()String actual = object1.toString();)。您的,然後子句將聲明String返回等於它應該,這可能是"3:30 PM"assertEquals("3:30 PM", actual);)。我沒有設計你的代碼,所以我不知道這是否正確,你可能會希望它返回'「Hello,World!」無論如何,我會先運行這一個;)