无法弄清楚JUnit测试用例失败的原因

时间:2013-09-14 20:41:19

标签: java function testing junit

我必须修改我的类,以便它通过我教授给我们的一堆JUnit测试用例。我已经移动了一些东西,以便变量应该更早地给出它们的正确值,但它仍然没有测试用例。我不知道该改变什么,所以它通过了。

这是一个测试用例类,我在所有6个测试中都失败了

package dayofweektesting;

import dayofweek.DayOfWeek;
import static org.junit.Assert.*;
import org.junit.Test;

/**
 * Test cases for "Day of week" lab assuming requirements set 
 * at top of DayOfWeek class
 * 
 * Test valid leap years
 * 
 *
 */
public class TestValidLeapYears {

    /**
     * Test valid 2/29/XXXX dates
     */
    @Test
    public void firstValidLeapYear() {
        DayOfWeek dow = new DayOfWeek(2, 29, 1904);
        assertTrue(dow.getDayOfWeek().compareTo("Monday") == 0);
        assertTrue(dow.getNumericDayOfWeek() == 2);
    }

    @Test
    public void secondValidLeapYear() {
        DayOfWeek dow = new DayOfWeek(2, 29, 1908);
        assertTrue(dow.getDayOfWeek().compareTo("Saturday") == 0);
        assertTrue(dow.getNumericDayOfWeek() == 0);
    }

    @Test
    public void thirdValidLeapYear() {
        DayOfWeek dow = new DayOfWeek(2, 29, 1912);
        assertTrue(dow.getDayOfWeek().compareTo("Thursday") == 0);
        assertTrue(dow.getNumericDayOfWeek() == 5);
    }

    @Test
    public void fourthValidLeapYear() {
        DayOfWeek dow = new DayOfWeek(2, 29, 1916);
        assertTrue(dow.getDayOfWeek().compareTo("Tuesday") == 0);
        assertTrue(dow.getNumericDayOfWeek() == 3);
    }

    /**
     * Test boundary dates of2/28/XXXX and 3/1/XXXX for valid leap years
     */
    @Test
    public void firstValidLeapYearBoundaries() {
        DayOfWeek dow = new DayOfWeek(2, 28, 1904);
        assertTrue(dow.getDayOfWeek().compareTo("Sunday") == 0);
        assertTrue(dow.getNumericDayOfWeek() == 1);
        dow = new DayOfWeek(3, 1, 1904);
        assertTrue(dow.getDayOfWeek().compareTo("Tuesday") == 0);
        assertTrue(dow.getNumericDayOfWeek() == 3);
    }

    @Test
    public void secondValidLeapYearBoundaries() {
        DayOfWeek dow = new DayOfWeek(2, 28, 1908);
        assertTrue(dow.getDayOfWeek().compareTo("Friday") == 0);
        assertTrue(dow.getNumericDayOfWeek() == 6);
        dow = new DayOfWeek(3, 1, 1908);
        assertTrue(dow.getDayOfWeek().compareTo("Sunday") == 0);
        assertTrue(dow.getNumericDayOfWeek() == 1);
    }

}

这是我必须修改的课程,以便传递所有内容

public class DayOfWeek1 {
    int myMonth, myDayOfMonth, myYear, myAdjustment, numericDayOfWeek, remainderSeven;
    /*
     * @param what the date was
     */
    public DayOfWeek1(int month, int dayOfMonth, int  year){
        myMonth = month;
        myDayOfMonth = dayOfMonth;
        myYear = year;
        remainderSeven = 0;
        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; 
        remainderSeven = numericDayOfWeek % 7;
    }
    /*
     * @return the numeric day of the week
     */
    public int getNumericDayOfWeek(){

        return remainderSeven;

    }
    /*
     * returns what day of the week it was
     */
     public String getDayOfWeek(){
         //numericDayOfWeek = this.getNumericDayOfWeek();
         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 :(得分:0)

你还没有指定你的JUnit测试会抛出什么错误,但我不得不猜测你猜错的原因是你使用DayOfWeek1作为你的类,你的JUnit测试正在测试一个类叫DayOfWeek。