我必须修改我的类,以便它通过 JUnit 测试用例。似乎一切都应该正常工作,但它一直在失败。它给了我一个 java.lang.AssertionError。
这是我的课
package dayofweek;
/**
* Method to find out what day of the week a certain date was
*
* Output determines what day of the week this certain date was
*
*/
public class DayOfWeek {
private int myMonth = -1, myDayOfMonth = -1, myYear= -1, myAdjustment= -1, numericDayOfWeek= -1, remainderSeven= -1;
private static final int NO_VALUE = -1;
/**
* @param what the date was
*/
public DayOfWeek(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 month in a string
*/
public String getMonthString(){
String[] arrayOfMonths = new String[12];
arrayOfMonths[0] = "January";
arrayOfMonths[1] = "February";
arrayOfMonths[2] = "March";
arrayOfMonths[3] = "April";
arrayOfMonths[4] = "May";
arrayOfMonths[5] = "June";
arrayOfMonths[6] = "July";
arrayOfMonths[7] = "August";
arrayOfMonths[8] = "September";
arrayOfMonths[9] = "October";
arrayOfMonths[10] = "November";
arrayOfMonths[11] = "December";
if (this.myMonth > 0 && this.myMonth <=12){
return arrayOfMonths[this.myMonth-1];
}
else{
return null;
}
}
/**
* @return what day of the month it was
*/
public int getDayOfMonth(){
if(myDayOfMonth != NO_VALUE){
return myDayOfMonth;
}
else{
return NO_VALUE;
}
}
/**
* @return what year it was
*/
public int getYear(){
if(myYear != NO_VALUE){
return myYear;
}
else{
return NO_VALUE;
}
}
/**
* @return the numeric day of the week
*/
public int getNumericDayOfWeek(){
return remainderSeven;
}
/**
* returns what day of the week it was
*/
public String getDayOfWeek(){
String[] arrayOfDays = new String[7];
arrayOfDays[0] = "Saturday";
arrayOfDays[1] = "Sunday";
arrayOfDays[2] = "Monday";
arrayOfDays[3] = "Tuesday";
arrayOfDays[4] = "Wednesday";
arrayOfDays[5] = "Thursday";
arrayOfDays[6] = "Friday";
if( myMonth != NO_VALUE && myDayOfMonth != NO_VALUE && myYear != NO_VALUE){
return arrayOfDays[remainderSeven];
}
else{
return null;
}
}
/**
* @return the month in an int
*/
public int getMonth(){
if(myMonth != NO_VALUE){
return myMonth;
}
else return NO_VALUE;
}
}
这是 JUnit 测试类
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);
}
}
这是错误指向的行,在 secondValidLeapYearBoundaries()
assertTrue(dow.getDayOfWeek().compareTo("Friday") == 0);
有谁知道如何解决所有这些问题?