• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Help fixing failing Test for Custom Date Util class.

 
James Dekker
Ranch Hand
Posts: 221
  • Mark post as helpful
  • send pies
  • 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.
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • 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: 221
  • Mark post as helpful
  • send pies
  • 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
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • 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: 221
  • Mark post as helpful
  • send pies
  • 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: 221
  • Mark post as helpful
  • send pies
  • 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!

 
Paul Clapham
Sheriff
Posts: 21322
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • 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"...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic