• 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

Help fixing failing Test for Custom Date Util class.

 
Ranch Hand
Posts: 231
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Am having trouble getting a test to pass with a custom date util class I created.

Here's the class (which I know works):



Here's the failing test case:



Its the testStartDate() method that fails:



(1) I know that the CustomDateUtil works but was wondering why I can't seem to get the test to pass?

(2) What's the best way to test multiple hard coded dates?

Thanks for taking the time to read this.
 
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Heck, testStartDate() won't even compile: please PostRealCode.

Did you check the millisecond values yourself to see if they're the same?
 
James Dekker
Ranch Hand
Posts: 231
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I did post real code... The only thing I didn't post was the imports... I copied and pasted the code from Eclipse. I am using JUnit 3.8.1.

Does the millisecond really have to be precise?

Does that mean that its impossible to test two generated Date references?
 
David Newton
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It compiles with the spurious comma?

Of course it doesn't mean you can't compare dates--it means if you do, and you're using its equals() method, that you have to follow its definition of equality.
 
James Dekker
Ranch Hand
Posts: 231
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
David,

Thanks for looking into this... I don't have the spurious comma in my code.
I accidentally inserted it there when I was editing my posting.

Of course it doesn't mean you can't compare dates--it means if you do, and you're using its equals() method, that you have to follow its definition of equality.



Can you please elaborate on this?

By the way, I commented out the first test method and the second one failed as well (with the same type of failure message).

Could anyone please point me in the right direction? What am I possibly doing wrong?
 
James Dekker
Ranch Hand
Posts: 231
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Got it working!

This posting, Compare Date Objects With Different Levels of Precision, from StackOverflow helped out a lot!

It was the milliseconds that played the problem.

Decided to use DateUtils.truncate() method from Apache Commons.

Here's the working code:



Now, if I break the test by setting DAY_OF_MONTH to 12, it displays this (take a look at the truncated seconds):



David thanks for all of your help and hopefully this posting will fall into the hands of someone else that might need compare dates using Java in the future!

 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's simple. The test failed because the two dates you created were not equal.

You printed out parts of those two dates, and the parts you printed were equal. So it follows that the parts you didn't
print were not equal. Hence David's reference to "milliseconds"...
 
reply
    Bookmark Topic Watch Topic
  • New Topic