-2

我必须修改我的课程,以便它通过我的教授给我们的一堆 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;
     }

}
4

1 回答 1

0

您尚未指定您的 JUnit 测试会抛出什么错误,但我不得不大胆猜测并猜测您得到的错误是因为您使用 DayOfWeek1 作为您的类,而您的 JUnit 测试正在测试一个名为 DayOfWeek 的类。

于 2013-09-14T23:48:17.043 回答