*
The moose likes Testing and the fly likes Assertion for Date Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » Testing
Bookmark "Assertion for Date" Watch "Assertion for Date" New topic
Author

Assertion for Date

A Kumar
Ranch Hand

Joined: Jul 04, 2004
Posts: 979
Hi all,

I am writing test cases and i wish to compare the date in the database

to what i entered in the DTO of the object...It is givind me errors..

I wish to comapre two dates...

Is there an assertionEquals or assertionTrue method or any other method..

that wud allow me..to do it. with the dates as arguments?

Thx in advance,
A Kumar.
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
As Date implements the equals method, assertEquals(Object, Object) should work fine...


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
A Kumar
Ranch Hand

Joined: Jul 04, 2004
Posts: 979
Hi,

I will try to work out this way..and get back to u...

But is it ok..if I compare the two dates...

int val= Date1.compareToDate(Date2)

assertEquals(1,val );

Thx
Lasse Koskela
author
Sheriff

Joined: Jan 23, 2002
Posts: 11962
    
    5
Originally posted by A Kumar:
But is it ok..if I compare the two dates...

int val= Date1.compareToDate(Date2)

assertEquals(1,val );

I would rather do something like this:


Author of Test Driven (2007) and Effective Unit Testing (2013) [Blog] [HowToAskQuestionsOnJavaRanch]
Lasse Koskela
author
Sheriff

Joined: Jan 23, 2002
Posts: 11962
    
    5
...or, if you want to test for equality:
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 30382
    
150

The other thing to watch with dates is the precision. ALl of the methods post about (that check for equality) require the dates to be the same to the millisecond. If you want to check just the date part or the time part, you can write a custom assertion. For example,



I favor assertEquals(date1, date2);
over assertTrue(date1.equals(date2));
because it gives clearer output on an assertion failure.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Assertion for Date