-2

所以我们创建了一个给定特定值的类,提出特定的东西。教授给了我们他一直在使用的测试用例,所以我们可以修复我们的测试用例,使它们匹配并正常工作。我在 eclipse 上,我试图在指定的包下创建新的 JUnit 测试用例,但我在测试用例类中不断收到很多错误,并且不允许它们作为 JUnits 运行。只要有“@”,例如“@Test”或“@Before”,它就会给出错误。即使我进入了窗口、首选项并单击了允许断言的框,也会在“断言”上给出错误。一开始我也收到所有“org.junits”的错误

这是我个人制作的课程,如果这很重要(而不是错误所在)

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-=1;
            }
        }
        if(myMonth==2){
            myAdjustment = 4;
            if(myYear%4==0){
                myAdjustment-=1;
            }
        }
        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;
     }

}

这是我必须在错误所在的地方使用的测试用例之一。还有几个喜欢的。

package dayofweektesting;

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

/**

 */

public class TestInvalidDates {

    @Before
    public void setUp() throws Exception {
    }

    @After
    public void tearDown() throws Exception {
    }

    /**
     * Test invalid date before first valid date
     */
    @Test
    public void beforeFirstDay() {
        DayOfWeek dow = new DayOfWeek(12, 31, 1899);
        this.assertBadDOW(dow);
    }

    /**
     * Test invalid date after last valid date
     */
    @Test
    public void afterLastDay() {
        DayOfWeek dow = new DayOfWeek(1, 1, 2100);
        this.assertBadDOW(dow);
    }

    /**
     * Test invalid date of Feb. 29th when not leap year
     * - year right after a leap year
     */
    @Test
    public void notLeapYearAfter() {
        DayOfWeek dow = new DayOfWeek(2, 29, 1909);
        this.assertBadDOW(dow);
    }

    /**
     * Test invalid date of Feb. 29th when not leap year
     * - year right between leap years
     */
    @Test
    public void notLeapYearMiddle() {
        DayOfWeek dow = new DayOfWeek(2, 29, 1910);
        this.assertBadDOW(dow);
    }

    /**
     * Test invalid date of Feb. 29th when not leap year
     * - year right after a leap year
     */
    @Test
    public void notLeapYearBefore() {
        DayOfWeek dow = new DayOfWeek(2, 29, 1911);
        this.assertBadDOW(dow);
    }

    /**
     * Test invalid 30th and 31st-day-of-month dates.
     *      2/30/2000
     *      2/31/2000
     *      4/31/2000
     *      6/31/2000
     *      9/31/2000
     *      11/31/2000
     */
    @Test
    public void badFebruary30() {
        DayOfWeek dow = new DayOfWeek(2, 30, 2000);
        this.assertBadDOW(dow);
    }

    @Test
    public void badFebruary31() {
        DayOfWeek dow = new DayOfWeek(2, 31, 2000);
        this.assertBadDOW(dow);
    }

    @Test
    public void badApril() {
        DayOfWeek dow = new DayOfWeek(4, 31, 2000);
        this.assertBadDOW(dow);
    }

    @Test
    public void badJune() {
        DayOfWeek dow = new DayOfWeek(6, 31, 2000);
        this.assertBadDOW(dow);
    }

    @Test
    public void badSeptember() {
        DayOfWeek dow = new DayOfWeek(9, 31, 2000);
        this.assertBadDOW(dow);
    }

    @Test
    public void badNovember() {
        DayOfWeek dow = new DayOfWeek(11, 31, 2000);
        this.assertBadDOW(dow);
    }

    /**
     * Test invalid day-of-month (negative)
     */
    @Test
    public void badDOMneg() {
        DayOfWeek dow = new DayOfWeek(1, -1, 2000);
        assertTrue(dow.getMonth() == DayOfWeek.NO_VALUE);
        assertTrue(dow.getMonthString() == null);
        this.assertBadDOW(dow);
    }

    /**
     * Test invalid day-of-month (0)
     */
    @Test
    public void badDOM0() {
        DayOfWeek dow = new DayOfWeek(1, 0, 2000);
        assertTrue(dow.getMonth() == DayOfWeek.NO_VALUE);
        assertTrue(dow.getMonthString() == null);
        this.assertBadDOW(dow);
    }

    /**
     * Test invalid day-of-month (32)
     */
    @Test
    public void badDOMafter() {
        DayOfWeek dow = new DayOfWeek(1, 32, 2000);
        assertTrue(dow.getMonth() == DayOfWeek.NO_VALUE);
        assertTrue(dow.getMonthString() == null);
        this.assertBadDOW(dow);
    }

    /*
     * Private helper method to check returned int and String values.
     */
    private void assertBadDOW(DayOfWeek dow)
    {
        assertTrue(dow.getDayOfWeek() == null);
        assertTrue(dow.getNumericDayOfWeek() == DayOfWeek.NO_VALUE);
    }

}

有谁知道如何解决这一问题?谢谢

4

1 回答 1

2

啊,刚刚发现你提到了 Eclipse。您应该能够转到项目属性 -> Java 构建路径 -> 库 -> 添加库... -> JUnit,并确保选择了某些内容(可能是 JUnit 4)。

于 2013-09-13T20:37:19.237 回答