I have this extra credit assignment that makes me do junit test but I don't understand how I can make my test for getDestinations return null. So I have this methods and variable:
private final Point3D destination = new Point3D();
public Point3D getDestination() {
if (destination == null) {
return null;
}
return new Point3D(destination);
}
public final void setDestination(Point3D aPoint) throws InvalidDataException {
if (aPoint == null) {
throw new InvalidDataException("Null Point3D sent to setDestination(Point3D)");
}
setDestination(aPoint.getX(), aPoint.getY(), aPoint.getZ());
}
I'm trying to make netbeans know that I when I test for destination = null it returns null.
Hers my test so far:
public void testGetDestination(){
testPoint3D = new Point3D(4.0, 5.0, 6.0);
Point3D p = testMovable.getDestination();
assertEquals(p, testPoint3D);
assertNotNull(p);
}
public void testSetDestination_Point3D() throws Exception {
Point3D newPoint = new Point3D(0.0, 0.0, 0.0);
testMovable.setDestination(newPoint);
Point3D p = new Point3D();
assertNotNull(p);
assertEquals(p, newPoint);
assertNotSame(p, newPoint);
p = null;
try{
testMovable.setDestination(p);
fail("Null Point3D sent to setDestination(Point3D)");
}catch(InvalidDataException ex){
assertEquals(ex.getMessage(),"Null Point3D sent to setDestination(Point3D)");
}
}
but as you can see I can't really invoke a null without having it to fail/caught by exceptions.
Is there a way to go about this?
Aucun commentaire:
Enregistrer un commentaire