0

Unit test fails yet biComplexNumber1.equals(biComplexNumber2) holds true.

Unit test:

 public void simpleTest(){
    BicomplexNumber b1 = new BicomplexNumber(1,2,3,4);
    BicomplexNumber b2 = new BicomplexNumber(1,2,3,4);
    assertEquals("Simple Test", b1, b2);
}

Equality check

public boolean equals(BicomplexNumber bicomplexObj) 
{
    if(bicomplexObj == this) return true;
    if(bicomplexObj.getA() != this.getA()) return false;
    if(bicomplexObj.getB() != this.getB()) return false;
    if(bicomplexObj.getC() != this.getC()) return false;
    if(bicomplexObj.getD() != this.getD()) return false;
    return true;
}

The equality check seems to be correct when I would like it too. But I have included it anyway.

ipro_ultra
  • 131
  • 2
  • 9
  • 3
    You're not overriding `equals(Object)`, which is the method that will be called by `assertEquals`. – Andy Turner Mar 27 '18 at 12:41
  • I changed object to BicomplexNumber in the method call because I need to use methods of BicomplexNumber in order to check for equality.. How can I do this for a generic object being passed into the method? – ipro_ultra Mar 27 '18 at 12:43
  • 1
    You have to take an `Object` parameter, check if it is a `BicomplexNumber`, and cast. See the question linked in my previous comment. – Andy Turner Mar 27 '18 at 12:48
  • @AndyTurner while this is a bad practice it should work. If `equals` is called with `BicomplexNumber` as an actual parameter, it should bind to that implementation. If it's called with a non matching type, the inherited `equals(Object)` should be called. – André Stannek Mar 27 '18 at 13:08
  • 1
    @AndréStannek "it should work" it doesn't, because `assertEquals(Object, Object)` invokes `equals(Object)`. You can define the `equals(BicomplexNumber)`, sure, you just can't expect it to be used in place of the former. – Andy Turner Mar 27 '18 at 13:36
  • I changed the parameter to an Object, and used the approach in the dupe. The unit test that I showed now works. Additionally the unit test for the thing I was already testing also passed! Thank you kind sir! – ipro_ultra Mar 27 '18 at 13:40
  • @AndyTurner nevermind, I didn't consider the hard cast to `Obect` during JUnit processing. – André Stannek Mar 27 '18 at 13:41

0 Answers0