测试日期实用程序

时间:2017-08-17 17:08:32

标签: java testing junit

我对JUnit测试很新,而且我在Java编程方面的背景并不牢固。我需要一位专家来帮助我正确运行测试。我希望你专注于我的参数,但如果有更好的建议,我愿意尝试。我只需要为ordinalDate()进行测试,谢谢。

DateUtilityTest.java

package week4;

import static org.junit.Assert.*;

import java.util.Arrays;
import java.util.Collection;
import java.util.List;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;

@RunWith(Parameterized.class)
public class DateUtilityTest {

DateUtility date = new DateUtility();

private int input;
private static int month;
private static int day;
private static int year;

public DateUtilityTest(int input, int month, int day, int year) {
    super();
    this.input = input;
    DateUtilityTest.month = month;
    DateUtilityTest.day = day;
    DateUtilityTest.year = year;
}

@Parameters
public static Collection<Object[]> <Integer> List<java.lang.Integer> testConditions() {
    Object input[][] = {
            {3, 16, 1993},
            {6, 24, 1997},
            {9, 8, 1995}
    };
    return Arrays.asList(month, day, year);
}

@Test
public void test() {
    assertEquals(input ,DateUtility.ordinalDate(month, day, year));
}

}

DateUtility.java

package week4;

public class DateUtility {
//Returns whether year is a leap year?
    public static boolean isLeapYear (int year)
    {return (year%4 == 0 && year%100 != 0) || year%400 == 0;}


  //Returns the number of days in month (in year)
    public static int daysIn (int month, int year) throws IllegalArgumentException
    {
      if (year < 1)
        throw new IllegalArgumentException("daysIn: year ("+year+") not positive");
      if (month < JANUARY || month > DECEMBER)
        throw new IllegalArgumentException("daysIn: month ("+month+") not in range [1,12]");

      //Thirty days hath September, April, June and November...
      if (month == APRIL     ||
          month == JUNE      ||
          month == SEPTEMBER ||
          month == NOVEMBER)
        return 30;

      //...all the rest have thirty one...
      else if (month == JANUARY || 
                     month == MARCH   ||
                     month == MAY     ||
                     month == JULY    ||
                     month == AUGUST  ||
                     month == OCTOBER ||
                     month == DECEMBER)
        return 31;

    //...except February (must be FEBRUARY in else: see possible exception)
      else 
        return 28 + (isLeapYear(year) ? 1 : 0);
    }


  //Returns the ordinal (1st, 2nd, 3rd, etc) representing month, day, year
    public static int ordinalDate (int month, int day, int year)
    {
      int ordinal = 0;

      //Scan every earlier month, summing the # of days in that month...
      for (int m=JANUARY;  m < month;  m++)
        ordinal += daysIn(m, year);

      //...and add day in the current month
      return ordinal + day;
    }



  //Returns a date as an American or European String

  public static String americanFormat (int month, int day, int year)
  {return month + "/" + day + "/" + year;}


  public static String europeanFormat (int month, int day, int year)
  {return day + "/" + month + "/" + year;}




  //Fields: all public static final (constants supplied by class)
  //These could be private, for use only in this class,
  //  but what the heck, let programmers use them from this class
  //  (as constants, there is nothing a programmer can do to mess things up)

  public static final int JANUARY   =  1;  
  public static final int FEBRUARY  =  2;  
  public static final int MARCH     =  3;  
  public static final int APRIL     =  4;  
  public static final int MAY       =  5;  
  public static final int JUNE      =  6;  
  public static final int JULY      =  7;  
  public static final int AUGUST    =  8;  
  public static final int SEPTEMBER =  9;  
  public static final int OCTOBER   = 10;  
  public static final int NOVEMBER  = 11;  
  public static final int DECEMBER  = 12;  

}

1 个答案:

答案 0 :(得分:2)

参数列表中的每个条目都是一个只有3个数字的数组

Object input[][] = {
        {3, 16, 1993},
        {6, 24, 1997},
        {9, 8, 1995}
};

而测试类的构造函数需要4个参数。

public DateUtilityTest(int input, int month, int day, int year)

我认为input的值缺失了。因此测试不会运行。