简体   繁体   中英

assertEquals fails on two Vectors

I want to write accelerate method for Ship class so I have written

public void accelerate(double fo){
    double newSpeedMag = speed.magnitude() + fo;
    speed = new Vector(Math.cos(facing) * newSpeedMag , Math.sin(facing) * newSpeedMag  );
}

Where facing is facing angle and currently facing == Math.PI .

In testAccelerate() test case:

testAccelerate(){
    s.facing = Math.PI/2;
    s.accelerate(4);
    assertEquals("wrong vector",new Vector(-3,4),s.speed);
}

assertEquals is failing.

It sounds like you most likely did not override equals for your class. Without this, there is no way for assertEquals to know if the classes are equal (well it tries, but will only return true if the point to the same memory address).

See this link for overriding equals on your class - http://users.csc.calpoly.edu/~kmammen/documents/java/howToOverrideEquals.html .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM