Java 测试日期实用程序的参数
我对JUnit测试非常陌生,而且我在Java编程方面的背景并不扎实。我需要一位专家来帮助我正确地进行测试。我希望你们关注我的参数,但如果有更好的建议,我愿意尝试。我只需要对Java 测试日期实用程序的参数,java,testing,junit,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.uti
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));
}
套餐周4;
导入静态org.junit.Assert.*;
导入java.util.array;
导入java.util.Collection;
导入java.util.List;
导入org.junit.Test;
导入org.junit.runner.RunWith;
导入org.junit.runners.Parameterized;
导入org.junit.runners.Parameterized.Parameters;
@RunWith(参数化的.class)
公共类DateUtilityTest{
DateUtility日期=新的DateUtility();
私有int输入;
私人静态整数月;
私人静态int日;
私人静态int年;
公共日期实用性测试(整数输入、整数月、整数日、整数年){
超级();
这个输入=输入;
DateUtilityTest.month=月份;
DateUtilityTest.day=天;
DateUtilityTest.year=年;
}
@参数
公共静态集合列表testConditions(){
对象输入[][]={
{3, 16, 1993},
{6, 24, 1997},
{9, 8, 1995}
};
返回数组.asList(月、日、年);
}
@试验
公开无效测试(){
assertEquals(输入,DateUtility.ordinalDate(月,日,年));
}
}
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;
套餐周4;
公共类实用程序{
//返回年份是否为闰年?
公共静态布尔值isLeapYear(整数年)
{返回(年份%4==0和年份%100!=0)| |年份%400==0;}
//返回月(年)中的天数
公共静态int daysIn(int月,int年)抛出IllegalArgumentException
{
如果(年份<1)
抛出新的IllegalArgumentException(“daysIn:year(“+year+”)非正”);
如果(月<一月|月>十二月)
抛出新的IllegalArgumentException(“daysIn:month(“+month+”)不在范围[1,12]”内;
//三十天有九月、四月、六月和十一月。。。
如果(月==四月)||
月份=六月||
月份=九月||
月份==11月)
返回30;
//…其余的都有三十一个。。。
如果(月==一月| |)
月份=三月||
月份=五月||
月份=七月||
月份==八月||
月份==十月||
月份==12月)
返回31;
//…二月除外(必须是二月,否则:请参阅可能的例外)
其他的
回报率28+(年?1:0);
}
//返回表示月、日、年的序号(第一、第二、第三等)
公共静态整数序号日期(整数月、整数日、整数年)
{
整数序数=0;
//每月扫描一次,将该月的天数相加。。。
对于(int m=一月;m
}参数列表中的每个条目都是一个只有3个数字的数组
Object input[][] = {
{3, 16, 1993},
{6, 24, 1997},
{9, 8, 1995}
};
而测试类的构造函数需要4个参数
public DateUtilityTest(int input, int month, int day, int year)
我认为input
的值缺失。因此,测试不会运行