如何判断JUnit测试出了什么问题?

时间:2013-09-13 23:25:58

标签: java testing junit case

我必须根据JUnit测试修复我的代码,但我不知道如何判断出错了什么?

例如,测试代码的这个功能在我运行时导致错误

@Test
    public void lastValidDay() {
        DayOfWeek dow = new DayOfWeek(12, 31, 2099);
        assertTrue(dow.getDayOfWeek().compareTo("Thursday") == 0);
        assertTrue(dow.getNumericDayOfWeek() == 5);
    }

不确定如何改变它以便它顺利运行?这是我的类,这些不同的JUnit正在测试中。这是我必须改变的类,所以所有测试都顺利进行。

public class DayOfWeek1 {
    int myMonth, myDayOfMonth, myYear, myAdjustment, numericDayOfWeek;
    /*
     * @param what the date was
     */
    public DayOfWeek1(int month, int dayOfMonth, int  year){
        myMonth = month;
        myDayOfMonth = dayOfMonth;
        myYear = year;
    }
    /*
     * @return the numeric day of the week
     */
    public int getNumericDayOfWeek(){
        if(myMonth==1){
            myAdjustment = 1;
            if(myYear%4==0){
                myAdjustment=0;
            }
        }
        if(myMonth==2){
            myAdjustment = 4;
            if(myYear%4==0){
                myAdjustment=3;
            }
        }
        if(myMonth==3){
            myAdjustment = 4;
        }
        if(myMonth==4){
            myAdjustment = 0;
        }
        if(myMonth==5){
            myAdjustment = 2;
        }
        if(myMonth==6){
            myAdjustment = 5;
        }
        if(myMonth==7){
            myAdjustment = 0;
        }
        if(myMonth==8){
            myAdjustment = 3;
        }
        if(myMonth==9){
            myAdjustment = 6;
        }
        if(myMonth==10){
            myAdjustment = 1;
        }
        if(myMonth==11){
            myAdjustment = 4;
        }
        if(myMonth==12){
            myAdjustment = 6;
        }
        int fourDivides = myYear / 4;
        numericDayOfWeek = myAdjustment + myDayOfMonth + (myYear-1900) + fourDivides; 
        return numericDayOfWeek;

    }
    /*
     * returns what day of the week it was
     */
     public String getDayOfWeek(){
         if(numericDayOfWeek==0){
             return "Saturday";
         }
         if(numericDayOfWeek==1){
             return "Sunday";
         }
         if(numericDayOfWeek==2){
             return "Monday";
         }
        if(numericDayOfWeek==3){
             return "Tuesday";
         }
        if(numericDayOfWeek==4){
            return "Wednesday";
         }
        if(numericDayOfWeek==5){
            return "Thursday";
         }
         if(numericDayOfWeek==6){
             return "Friday";
         }
         else{
             return "NO_VALUE";
         }
     }
     /*
      * @return the month in an int
      */
     public int getMonth(){
         return myMonth;
     }
     /*
      * @return the month in a string
      */
     public String getMonthString(){
         if(myMonth==1){
             return "January";
         }
         if(myMonth==2){
             return "February";
         }
         if(myMonth==3){
             return "March";
         }
         if(myMonth==4){
             return "April";
         }
         if(myMonth==5){
             return "May";
         }
         if(myMonth==6){
             return "June";
         }
         if(myMonth==7){
             return "July";
         }
         if(myMonth==8){
             return "August";
         }
         if(myMonth==9){
             return "September";
         }
         if(myMonth==10){
             return "October";
         }
         if(myMonth==11){
             return "November";
         }
         if(myMonth==12){
             return "December";
         }
         else{
             return "NO_VALUE";
         }
     }
     /*
      * @return what day of the month it was
      */
     public int getDayOfMonth(){
         return myDayOfMonth;
     }
     /*
      * @return what year it was
      */
     public int getYear(){
         return myYear;
     }

}

关于弄清楚我的代码究竟是什么导致错误发生的任何提示?

1 个答案:

答案 0 :(得分:2)

问题是您在getDayOfWeek()之前呼叫getNumbericDayOfWeek()。在构造函数中,您不初始化numericDayOfWeek,因此它获得默认值0(所有未初始化的基元类型都可以)。

public DayOfWeek1(int month, int dayOfMonth, int  year){
    myMonth = month;
    myDayOfMonth = dayOfMonth;
    myYear = year;
}

然后致电

assertTrue(dow.getDayOfWeek().compareTo("Thursday") == 0);

将返回

 public String getDayOfWeek(){
     if(numericDayOfWeek==0){
         return "Saturday";
     }

"Saturday"compareTo("Thursday")未返回0,因此您的测试失败。{/ p>

您需要先调用getNumericDayOfWeek()首先将numericDayOfWeek字段设置为其真实语义值。


关于实际问题

  

有关确定我的代码中究竟是什么的任何提示   错误消失了?

断言声明会告诉你条件是错误的。从那里开始,你的第一直觉应该是吝啬dow.getDayOfWeek()返回的内容,因为它不等于"Thursday"。解决这个问题的一种方法是做

assertEquals("Thursday", dow.getDayOfWeek());

它会立即告诉您预期和实际不同以及它们的价值是什么。您会看到dow.getDayOfWeek()返回"Saturday",然后您可以看到numericDayOfWeek的值为0,依此类推。