I'm making a JUnit test that takes in a Person and checks if a PersonException (my custom exception) is returned. PersonException extends Exception and Person has a method SetDOB() that throws a PersonException if the individual is over 100. So far, the exception prints in the console, as it should, but for some reason still fails the test. Here's the relevant code.
//JUnit Test
@Test(expected = PersonException.class)
public void testBadShieffer1() {
Staff wrongShieffer = new Staff("Bob", "Lloyd", "Shnieffer",
new Date(1737, 2, 25), "Shieffer Lane", "21277777778" /*PhoneNumber*/ ,
"RadioMan@shieffer.shief",
"Radio time.", 10, 500000.00, new Date(1991 + 1900, 1, 1), eTitle.MR);
//This constructor calls SetDOB()
fail("No PersonExceptions were thrown.");
}
//setDOB
public void setDOB(Date DOB) {
try {
if (((new Date()).getYear() + 1900) > DOB.getYear() + 100)
throw new PersonException(this);
else
this.DOB = DOB;
} catch (PersonException p) {
System.out.println(p + " is over 100 years old!");
}
}
//PersonException class
public class PersonException extends Exception {
private Person p;
public PersonException() {
super();
}
public PersonException(String message) {
super(message);
}
public PersonException(Person p) {
super(p.getLastName());
this.p = p;
}
public Person P() {
return p;
}
}
I apologize if this is a tad lengthy, but I believe everything that needs to be in there is present. Thanks in advance for any help!