tky*_*ass 5 java junit timestamp
I'm trying to write a Junit for testing an object that has the current timestamp. the problem is when I first create the current timestamp as the following:
Date date = new Date();
Timestamp timestamp = new Timestamp(date.getTime());
Run Code Online (Sandbox Code Playgroud)
It creates a timestamp object with current time including the milliseconds. so when I run my tests they're never equal because there is some time elapsed in milliseconds between the time I created the timestamp object and the time of testing:
x.setTimeToMyMethod(timestamp);
assertEquals(timestamp, x.getTimeFromMyMethod());
Run Code Online (Sandbox Code Playgroud)
I tried to use SimpleDateFormat but it converts the time to a string and my setter method only accept Timestamp objects. Is there anyway that I can overcome this problem? Thanks in advance!
Thanks for the answers and here is my code and hopefully it will clarify what I'm trying to do:
public class myTest {
Date date = new Date();
Timestamp timestamp = new Timestamp(date.getTime());
myTestingClass testClass = new myTestingClass();
@BeforeClass
public static void setUpBeforeClass() throws Exception {
}
@AfterClass
public static void tearDownAfterClass() throws Exception {
}
@Before
public void setUp() throws Exception {
testClass.setCreateDate(timestamp);
}
@After
public void tearDown() throws Exception {
}
@Test
public void test() {
try {
assertEquals(timestamp, testClass.getCreateDate());
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
Run Code Online (Sandbox Code Playgroud)
when I try to run it it gives me the following error:
java.lang.AssertionError: expected:<2015-02-11 09:09:40.038> but was:<2015-02-11 09:09:40.04>