1

I did an assertEqual test and it fails even though it showing that the expected and actual results are the same.I looked it up and I saw that you need to check that the compared objects are of the same type,in this case,they are but it still fails,this it the test:

  public void testConstructRayThroughPixel() {
    System.out.println("constructRayThroughPixel");
    int Nx = 3;
    int Ny = 3;
    double x = 3.0;
    double y = 3.0;
    double screenDistance = 100.0;
    double screenWidth = 150.0;
    double screenHeight = 150.0;
    Camera instance = new Camera();
    Ray expResult = new Ray(new Point3D(new Coordinate(50),new Coordinate(-50),new Coordinate(-100)),new Vector(new Point3D(new Coordinate(50),new Coordinate(-50),new Coordinate(-100))));
    Ray result = instance.constructRayThroughPixel(Nx, Ny, x, y, screenDistance, screenWidth, screenHeight);
    assertEquals(expResult, result);

}

this is the result (Uploaded an image): assertEqual result

To the guys editing here,I don't know how it has anything to do with double type,this is a comprehension between two objects of "Ray"..

RT.
  • 423
  • 3
  • 12
  • `assertEquals()` relies on the implementation of `equals` in the objects compared. If you did not implement it your self two different objects are never *equal*. – Timothy Truckle Apr 26 '17 at 17:24
  • Thanks for your comment.You mean that I should override it like toString ? – RT. Apr 26 '17 at 17:26
  • 1
    @R.p Yes, you need to override `equals()`. – Code-Apprentice Apr 26 '17 at 17:28
  • 1
    Yes, but keep in mind that `equals()` always goes together with `hashcode()`! http://tutorials.jenkov.com/java-collections/hashcode-equals.html – Timothy Truckle Apr 26 '17 at 17:28
  • See also [the documentation for `equals()`](https://docs.oracle.com/javase/7/docs/api/java/lang/Object.html#equals(java.lang.Object)). In particular, "Note that it is generally necessary to override the hashCode method whenever this method is overridden, so as to maintain the general contract for the hashCode method, which states that equal objects must have equal hash codes." – Code-Apprentice Apr 26 '17 at 17:31
  • I changed the "duplicated to" question. Probably the first one was just a mistake. This is really basic stuff: as already the NAME indicates, assertEquals() calls uses the equals() method to compare two objects. – GhostCat Apr 26 '17 at 19:28
  • I added those functions using the automatic "insert" option on NetBeans, and it still gives the same result,"Expected" and "was"(actual value) is the same,but the test is under "fail". – RT. Apr 27 '17 at 09:33

0 Answers0