0

I am developping something which keeps the current age of the oldest item, for code coverage I write JUnit tests.

So when I write my unit tests, I perform following action:

currentAge = resultSet.getTime(4);
Calendar cal = Calendar.getInstance();
        Assert.assertEquals(new Time(cal.get(Calendar.HOUR_OF_DAY) - 1,cal.get(Calendar.MINUTE) - 30,Calendar.SECOND), currentAge);

This actualy works, but I get sometimes this test failure

junit.framework.AssertionFailedError:
Expected :08:09:14

Actual :08:09:13

I think the reason is that the sequence of the actions maybe makes sometimes 1 second difference. Is there any way to round this in a good way or making the test to perform that 1 second difference is allowed?

What should I do?

Jonas Audenaert
  • 160
  • 2
  • 16

1 Answers1

3

Use the delta argument to your assertEquals method - see this answer and this javadoc

Community
  • 1
  • 1
TrueDub
  • 5,000
  • 1
  • 27
  • 33
  • can you give me of an example with a delta with 1 second, used in java.sql.time object? – Jonas Audenaert Mar 23 '15 at 09:04
  • What type is your currentAge variable? I'd suggest using cal.getTimeinMillis() to get a long number, doing the same for your currentAge variable and then comparing using assertEquals with 1000L as your delta value (1000 milliseconds = 1 second) – TrueDub Mar 23 '15 at 09:54
  • ok good answer. My currentAge is a java.sql.time, which contains a long method getTime() to get the milis. :-) – Jonas Audenaert Mar 23 '15 at 09:59