• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Jeanne Boyarsky
  • Tim Cooke
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Mikalai Zaikin
  • Carey Brown
Bartenders:

Working of AssertEquals(Object, Object) in JUnit

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm just curious about the workings of the JUnit AssertEquals method when comparing two of my own classes where I have overridden the equals method and I was trying to work out why my unit test for it kept failing. My example is as follows:



I thought that the AssertEquals method should just be using the equals method and the implementation in my classes would be called. However, in the above example, the assertTrue passes, but the AssertEquals fails. Debugging the assertEquals call, it never actually calls into my equals method.
  • Is there something wrong with my understanding of what's going on here?
  • Am I likely to have missed something that might catch me out later?
  • Is it generally better to avoid using AssertEquals for Object?

  • Thanks for any thoughts you might have on this.

    [ July 29, 2007: Message edited by: Stephen Masters ]
    [ July 29, 2007: Message edited by: Stephen Masters ]
     
    Rancher
    Posts: 43081
    77
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Welcome to JavaRanch.

    Are you overriding the hashCode method? You generally need to do that if you override equals; read the javadocs of both methods for some information.
     
    Stephen Masters
    Greenhorn
    Posts: 6
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Yep ... I have also overridden the hashCode() method, but that's not getting called by assertEquals either!
     
    author
    Posts: 14112
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    What version of JUnit are you using? Can you show us the code for BigDecimalMatrix?
     
    Stephen Masters
    Greenhorn
    Posts: 6
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Looks like it's 4.3.1 - the one that comes bundled with eclipse 3.3.

    The whole class is fairly long so I'll just throw in the more relevant bits (I think!). i.e. I won't bother putting in the methods for adding, subtracting, etc. It may be worth noting that Matrix is an abstract class.
     
    Stephen Masters
    Greenhorn
    Posts: 6
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Sorry guys ... looks like I've worked it out. I just changed it as follows and it's happy now:

    Obviously, when I was calling .equals(Matrix), it was working fine. However, I'm assuming that assertEquals must be passing Object everywhere. My class didn't have a .equals(Object) method and was therefore falling over. I guess all I need to do now is check whether it's an instance of Matrix before casting and it should be fine.

    Thanks for taking a look anyway!
     
    Ilja Preuss
    author
    Posts: 14112
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    With other words, you didn't override the equals method, you overloaded it.

    Nice to see that you've found the problem!
     
    You firghten me terribly. I would like to go home now. Here, take this tiny ad:
    a bit of art, as a gift, the permaculture playing cards
    https://gardener-gift.com
    reply
      Bookmark Topic Watch Topic
    • New Topic