lundi 12 septembre 2016

Java assertFalse() not working correctly

Having some problems getting assertFalse to work. I can't seem to see anything wrong but i am also just a beginner. I'm basically passing three variables and then checking to see if they meet the requirements. However, if only one variable is incorrect will still return false even when it should be true. If all three variables are incorrect then it will return false like it should. The assertTrue works correctly and will return an error if a wrong variable is given. I thought it might be the way my if statements have been setup but I do not see any flaw in them.

Here is my main

public class Date {

private int month; // 1-12
private int day;    // 1-30
private int year; // cannot be negative

public boolean validateDate() {

    if (this.month <= 0) {
        return (false);
    }  else if (this.month >= 13) {
        return (false);
    }

    if (this.day <= 0) {
        return (false);
    } else if (this.day >= 31) {
        return (false);
    }

    if (this.year <= 0) {
        return (false);
    }

    return (true);
}

public boolean setDate(int month, int day, int year) {
    this.month = month;
    this.day = day;
    this.year = year;

    if ( !validateDate())
        return (false);

    return (true);
}

and here is my testing

public class DateTest {

@Test
public void testSetDate() {

    Date dateClass = new Date();

    assertFalse( dateClass.setDate(-1, -1, -1) );
    assertFalse( dateClass.setDate(13, 31, 1995) );
    assertFalse( dateClass.setDate(20, 35, 2016) );

    assertTrue(dateClass.setDate(1, 20, 1907));
    assertTrue(dateClass.setDate(9, 8, 2016));
    assertTrue(dateClass.setDate(12, 25, 1995));
    }

}

All help is appreciated.

Aucun commentaire:

Enregistrer un commentaire