• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Unit Test compareTo / Comparable

 
Ranch Hand
Posts: 15304
6
Mac OS X IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is probably a dumb question but I can't find anything for JUnit or TestNG that allows me to assertCompareble or assertCompareTo or something similar. Furthermore I am not even sure if such things should be tested.

Can someone point me in the right direction?

Thanks.
 
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Gregg,
I don't follow what you are asking for. The following checks objects are comparable. I suspect you means something else though.

assertEquals("comparable", 0, obj1.compareTo(obj2));
 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Mac OS X IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Jeanne Boyarsky:
Gregg,
I don't follow what you are asking for. The following checks objects are comparable. I suspect you means something else though.

assertEquals("comparable", 0, obj1.compareTo(obj2));



And that's what I was looking for. Thanks.
 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Mac OS X IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I was looking at the API. How do you know to put "comparable" in for the message? What are the other message options?
 
Ranch Hand
Posts: 234
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Gregg Bolinger:
I was looking at the API. How do you know to put "comparable" in for the message? What are the other message options?



The message is just a reference that is printed when the test fails. There are no special options and it has nothing to do with the actual test.
Or am I misinterpreting the question?
 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Mac OS X IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by bart zagers:


The message is just a reference that is printed when the test fails. There are no special options and it has nothing to do with the actual test.
Or am I misinterpreting the question?



Nope, works for me. So I was doing..

Assert.assertTrue(obj1.compareTo(obj2) == 0);

Is there any advantage/disadvantage to this over Jeanne's suggestion?
 
author
Posts: 11962
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Gregg Bolinger:
Assert.assertTrue(obj1.compareTo(obj2) == 0);

Is there any advantage/disadvantage to this over Jeanne's suggestion?


Well, when your assertion fails you only hear "AssertionFailedError". When Jeanne's assertion fails she hears "AssertionFailedError: comparable".

Especially if you're using this assertion more than once, I'd recommend considering a "custom" assertion method such as this:
 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Mac OS X IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks. I'll try that.
 
Jeanne Boyarsky
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Lasse Koskela:
When Jeanne's assertion fails she hears "AssertionFailedError: comparable".


Also note that "Jeanne's assertion" uses assertEquals() rather than assertTrue(). This means that I get the actual value in the message. For comapreTo, I can see if it is larger or smaller than the expected 0 which gives me an extra clue before I look to see what went wrong.

Lasse's message is certainly even more descriptive.
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Note that the tests suggested so far don't really fully test that compareTo is working as expected - for that you needed to test different scenarios.

Take a look at http://junit-addons.sourceforge.net/junitx/extensions/ComparabilityTestCase.html to simplify that task. (You might also want to take a look at the source code to learn how it's done.)
 
Jeanne Boyarsky
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Ilja Preuss:
Note that the tests suggested so far don't really fully test that compareTo is working as expected - for that you needed to test different scenarios.


Oh! I thought I wasn't understanding the question. Your explanation of it makes sense as for the real question.
 
Thanks tiny ad, for helping me escape the terrible comfort of this chair.
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic