aspose file tools*
The moose likes Testing and the fly likes Help fixing failing Test for Custom Date Util class. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » Testing
Bookmark "Help fixing failing Test for Custom Date Util class." Watch "Help fixing failing Test for Custom Date Util class." New topic
Author

Help fixing failing Test for Custom Date Util class.

James Dekker
Ranch Hand

Joined: Dec 09, 2006
Posts: 219
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

Joined: Sep 29, 2008
Posts: 12617

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

Joined: Dec 09, 2006
Posts: 219
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

Joined: Sep 29, 2008
Posts: 12617

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

Joined: Dec 09, 2006
Posts: 219
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

Joined: Dec 09, 2006
Posts: 219
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
Bartender

Joined: Oct 14, 2005
Posts: 18909
    
    8

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"...
 
 
subject: Help fixing failing Test for Custom Date Util class.