尝试使用JUnit测试用例时有很多错误

时间:2013-09-13 20:17:27

标签: java eclipse testing junit case

所以我们创建了一个给出某些值的类,提出了某些东西。教授给了我他一直使用的测试用例,所以我们可以修复我们的测试用例,以便他们匹配并正常工作。我正在使用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);
    }

}

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

1 个答案:

答案 0 :(得分:2)

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